Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - When to use ->get()

Tags:

laravel

I'm confused as to when ->get() in Laravel...

E.G. DB::table('users')->find(1) doesn't need ->get() to retrieve the results, neither does User::find(1)

The laravel docs say "...execute the query using the get or first method..."

I've read the Fluent Query Builder and Eloquent docs but don't understand when the usage of get() is required...

Thanks for the help enter image description here

like image 814
Bill Jobs Avatar asked Dec 04 '12 07:12

Bill Jobs


People also ask

What is difference between GET and all in Laravel?

get() is used when you want to add queries and all() is used to get all data, without using any condition.

What is first () in Laravel?

The Laravel Eloquent first() method will help us to return the first record found from the database while the Laravel Eloquent firstOrFail() will abort if no record is found in your query. So if you need to abort the process if no record is found you need the firstOrFail() method on Laravel Eloquent.


4 Answers

Since the find() function will always use the primary key for the table, the need for get() is not necessary. Because you can't narrow your selection down and that's why it will always just try to get that record and return it.

But when you're using the Fluent Query Builder you can nest conditions as such:

$userQuery = DB::table('users');
$userQuery->where('email', '=', '[email protected]');
$userQuery->or_where('email', '=', '[email protected]');

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

// Done with building the query
$users = $userQuery->get();
like image 179
Niklas Modess Avatar answered Oct 05 '22 04:10

Niklas Modess


  • For find(n), you retrieve a row based on the primary key which is 'n'.
  • For first(), you retrieve the first row among all rows that fit the where clauses.
  • For get(), you retrieve all the rows that fit the where clauses. (Please note that loops are required to access all the rows or you will get some errors).
like image 42
Han Lim Avatar answered Oct 05 '22 04:10

Han Lim


find returns one row from the database and represent it as a fluent / eloquent object. e.g. SELECT * FROM users WHERE id = 3 is equivalent to DB::table('users')->find(3);

get returns an array of objects. e.g. SELECT * FROM users WHERE created_at > '2014-10-12' is equivalent to DB::table('users')->where('created_at', '>', '2014-10-12')->get() will return an array of objects containing users where the created at field is newer than 4014-10-12.

like image 38
Gravy Avatar answered Oct 05 '22 03:10

Gravy


The get() method will give you all the values from the database that meet your parameters where as first() gets you just the first result. You use find() and findOrFail() when you are searching for a key. This is how I use them:

When I want all data from a table I use the all() method

Model::all();

When I want to find by the primary key:

  Model::find(1)->first();

Or

 Model::findOrFail(1)->first();

This will work if there is a row with a primary key of one. It should only retrieve one row so I use first() instead of get(). Remember if you deleted the row that used key 1, or don't have data in your table, your find(1) will fail.

When I am looking for specific data as in a where clause:

Model::where('field', '=', 'value')->get();

When I want only the first value of the data in the where clause.

Model::where('field', '=', 'value')->first();
like image 28
Dom DaFonte Avatar answered Oct 05 '22 02:10

Dom DaFonte