What is difference between these two in laravel
$input = Input::get();
And
$input = Input::all();
And which one i should prefer.
Taken from the laravel source:
public static function all()
{
$input = array_merge(static::get(), static::query(), static::file());
// ....
return $input;
}
So all() calls get() and returns it's contents along with query(), and file() the $_FILES superglobal.
Preference will obviously depend on circumstance. I personally choose to use Input::get($key, $default) as I usually know what I am after.
From the Laravel Manual: http://laravel.com/docs/input
Retrieve a value from the input array:
$email = Input::get('email');
Note: The "get" method is used for all request types (GET, POST, PUT, and DELETE), not just GET requests.
Retrieve all input from the input array:
$input = Input::get();
Retrieve all input including the $_FILES array:
$input = Input::all();
By default, null will be returned if the input item does not exist. However, you may pass a different default value as a second parameter to the method:
Related
I need to add custom data to the request input array from my customRequest class
I tried this way
request()->request->add(['cool' => request()->get('var1').request()->get('var2')]);
It's do the trick with request()->all() but when I returned $request->validated() it's not exist.
how can I do it?
$request->validated() is returning only validated data (data in the request validator class).
After validating the data you can add additional data in the request using
$request->merge(['cool' => request()->get('var1')]);
Laravel documentation: https://laravel.com/docs/8.x/requests#merging-additional-input
I had the same problem and this is what I did, and it worked for me.
You can store the validated data in a variable as shown below.
$validated_data = $request->validated();
And then make your changes on the $validated_data variable as shown below.
$validated_data['cool'] = $request->input('var1').$request->input('var2');
This should add the extra data to the validated data.
I have done this way
protected function passedValidation()
{
$bar = 'test';
$validated = $this->validated();
$validated['foo'] = $bar;
$this->merge([
'mergedValidated' => $validated
]);
}
Then in Controller I did this
$request->mergedValidated
You can merge with new array
array_merge(request()->all(), ['cool' => request()->get('var1').request()->get('var2')]);
I would like to submit my form with many of fields.
As the documentation
$flight = new Flight;
$flight->name = $request->name;
$flight->param1 = $request->param1;
$flight->param2 = $request->param2;
...
$flight->param_n = $request->param_n;
$flight->save();
Its a bad idea if have too much fields.
I'm looking for any script like:
$flight = new Flight;
$flight->save($request->all());
But $request->all() function got unnecessary fields
What is the best way to do?
You could use the model $fillable array for this so long as your model properties match your request properties exactly.
$flight = new Flight();
$data = $request->only($flight->getFillable());
$flight->fill($data)->save();
You'll need to specify the fillable fields for any model that you would like to use this behavior for.
For Laravel 5.4 and lower use intersect instead of only
Otherwise you can just whitelist the properties you want from the request
$data = $request->only(['param1', 'param2' ...]);
There are various ways. you can exclude unwanted values as
$data = $request->except(['_token','_method','etc']);
The best way would be validated data. viz apply validation on your form inputs on server side.
$validated_data = $request->validate(['field1'=>'required','field2'=> 'required']);
etc. you can apply desired validations on each field and only validated fields will be in $validated_data variable, and then you can save them.
I am following tutorial http://www.tutorials.kode-blog.com/laravel-5-angularjs-tutorial and I have managed to write the similar method for my controller:
public function update(Request $request, $id) {
$employee = Employee::find($id);
$employee->name = $request->input('name');
//...
$employee->save();
return "Sucess updating user #" . $employee->id;
}
It is thought in tutorial that this code works but in reality var_dump($request->input) gives NULL. So - what $request variable should I use for getting the body of the request? I made var_dump($request) but the structure is unmanageably large. Actually I am suscpicous about this tutorial - do we really need to list all the fields in the standard update procedure?
You can access the input data with:
$input = $request->all();
https://laravel.com/docs/5.2/requests#retrieving-input
However, I've also had to get the input in this manner when using AngularJS $http module:
$input = file_get_contents('php://input');
for get all input
try it
$request = \Input::all();
If you want to fetch individual parameters from request object the you can do that with input Method of Request Class.
$request->input("parameter_name");
But if you want to fetch all request parameters then you can use all method which will return you an array of all the request key-value pairs
$request->all()
The thing you are missed is, you are calling $request->input which is null because input is method of Request class and not a property
I have a Validator class that can build several arrays with methods (names) stored.
Like so $this->rules[1] = ['trim()', 'required()', 'max(35)'];
How can I loop through every method the array and call it exactly by how they are defined?
If I do it like the following, I get Undefined property: Validator::$trim() etc. error.
foreach ($this->rules[1] as $method) {
$this->$method;
}
How can I add an extra parameter $input to each method in the array before it gets in the loop?
So instead of trim() it would be trim($input), and for max(35) max(35, $input), etc.
First of all, use $this->{$method} to call your method in your example.
Secondly, don't call them like that at all. Use call_user_func_array instead.
You need to extract method name and parameters frist in order to call directly.
I recommend you use a placeholder for your $input to add it to your method call.
You can then pass the parameters for your function call as an array.
$ruleset = 'max(34,:input)';
// do the string manipulation yourself please ;-)
$method = 'max';
$input = getInput(); // wherever you get that from
$parameters = array (34, $input),
call_user_func_array(__NAMESPACE__ .'\Validator::' . $method, $parameters);
What you are looking for are the call_user_func and/or call_user_func_array methods.
As Andresch pointed out, the way your rules are defined aren't flexible. Now you would have to parse them to retrieve the inputs for the function. a better way would be the following format:
$this->rules[1] = array(
'trim',
'required',
array('max'=>35)
);
and then
foreach ( $this->rules as $rule )
{
if ( is_array($rule)
{
call_user_func_array(array($this, key($rule)), current($rule));
}
else
{
call_user_func(array($this,$rule));
}
}
P.S. don't just copy paste this code, this is intended for explanation
I am trying to create an MVC application and am currently working on the Bootstrap file. I get the URL and explode it then assign the parts to the Controller Method and Method arguments. But, I can't find a way to pass multiples arguments to the method.
mysite/newuser/login/user_name/user_pass
newuser -> Controler of the site
login -> currently used method
user_name -> first argument
user_pass -> second_argument
For example
$url = "mysite/newuser/login/user_name/user_pass";
$path = expload('/',$url);
$this->controler = $path[0];
$this->method = $path[1];
For the arguments I create a second array like this:
// Set the substring path as method properties
if (isset($path[2])) {
$this->url_sub_path = $path[2];
$sub_path = explode('/', $this->url_sub_path);
if (isset($sub_path)) {
$this->model_properties = $sub_path;
When i assign the controllers a set
$site_controler = $this->controler;
include CONTROLER.$site_controler . '.php';
$new_instans = new $site_controler();
But the problem is here:
$site_method = $this->model;
$new_instans->{$site_method}($this->model_properties);
$this->model_properties is Array
if the function is:
public function login($user_name,$user_pass){
// some code
}
I need to pass the url properties they are Array and I have two variables in my function
The idea is to convert the array to variables
Or you could pass an idea in how to pass arguments from URL to my model
Try this function: http://www.php.net/manual/en/function.call-user-func-array.php
call_user_func_array ($new_instans->{$site_method} , $this->model_properties )
If, as you say, $this->model_properties is an array, you can do one of two things.
Case 1: Maintain the function declaration, and access the array's elements before calling the function.
The login() function (maintain it's declaration):
public function login($user_name,$user_pass){
// some code
}
To call the function, do this:
$array = $this->model_properties;
$param1 = $array[0]; //The numeric index may vary, depending on how this array was populated
$param2 = $array[1];
$new_instans->{$site_method}($param1, $param2);
Case 2: Change the function declaration to receive an array, and access the array's elements inside the function.
The login() function, change the declaration:
public function login($arrayParams){
//Access the parameters like this
$param1 = $arrayParams[0]; //The numeric index may vary, depending on how this array was populated
$param2 = $arrayParams[1];
//The rest of your code...
}
To call the function simply pass the array, as you're already doing:
$new_instans->{$site_method}($this->model_properties);
Independently of which version you choose to solve your problem, the important part is this:
$param1 = $array[0];
$param2 = $array[1];
Here, you assign the content of the array's elements of index 0 and 1 to a variable, allowing you to treat those values independently.