Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between get() and all() in laravel

What is difference between these two in laravel

$input = Input::get();

And

$input = Input::all();

And which one i should prefer.

like image 456
Muhammad Raheel Avatar asked Mar 12 '13 13:03

Muhammad Raheel


People also ask

What is the difference between GET and all in laravel?

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.

What does get () do in laravel?

This allows you to add conditions throughout your code until you actually want to fetch them, and then you would call the get() function.

What is difference between GET and first in laravel?

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.

What is difference between Pluck and select in laravel?

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.


2 Answers

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.

like image 165
juco Avatar answered Oct 07 '22 07:10

juco


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:

like image 3
Grant Avatar answered Oct 07 '22 07:10

Grant