What is difference between these two in laravel
$input = Input::get();
And
$input = Input::all();
And which one i should prefer.
all() is a static method on the Eloquent\Model . All it does is create a new query object and call get() on it. With all() , you cannot modify the query performed at all (except you can choose the columns to select by passing them as parameters). get() is a method on the Eloquent\Builder object.
This allows you to add conditions throughout your code until you actually want to fetch them, and then you would call the get() function.
The first() method will return only one record, while the get() method will return an array of records that you can loop over. Also, the find() method can be used with an array of primary keys, which will return a collection of matching records.
Pluck function normally used to pull a single column from the collection or with 2 columns as key, value pairs, which is always be a single dimension array. Select will return all of the columns you specified for an entity in a 2 dimensional array, like array of selected values in an array.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With