Laravel - 何时使用 ->get()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13697981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel - When to use ->get()
提问by Bill Jobs
I'm confused as to when ->get()
in Laravel...
我很困惑什么时候->get()
在 Laravel ......
E.G. DB::table('users')->find(1)
doesn't need ->get() to retrieve the results, neither does User::find(1)
EGDB::table('users')->find(1)
不需要 ->get() 来检索结果,也不需要User::find(1)
The laravel docs say "...execute the query using the get or first method..."
Laravel 文档说“...使用 get 或 first 方法执行查询...”
I've read the Fluent Query Builder and Eloquent docs but don't understand when the usage of get() is required...
我已阅读 Fluent Query Builder 和 Eloquent 文档,但不明白何时需要使用 get() ...
Thanks for the help
谢谢您的帮助
回答by Niklas Modess
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.
由于该find()
函数将始终使用表的主键,因此不需要get()
。因为您无法缩小选择范围,这就是为什么它总是会尝试获取该记录并返回它。
But when you're using the Fluent Query Builder you can nest conditions as such:
但是当您使用 Fluent Query Builder 时,您可以嵌套条件,如下所示:
$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.
这允许您在整个代码中添加条件,直到您真正想要获取它们,然后您将调用该get()
函数。
// Done with building the query
$users = $userQuery->get();
回答by Han Lim
- For
find(n)
, you retrieve a row based on the primary keywhich is 'n'. - For
first()
, you retrieve the first rowamong all rows that fit the where clauses. - For
get()
, you retrieve all the rowsthat fit the where clauses. (Please note that loops are required to access all the rows or you will get some errors).
- 对于
find(n)
,您根据主键“n”检索一行。 - 对于
first()
,您检索所有符合 where 子句的行中的第一行。 - 对于
get()
,您检索适合 where 子句的所有行。(请注意,访问所有行都需要循环,否则会出现一些错误)。
回答by Dom DaFonte
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:
该get()
方法将为您提供数据库中满足您的参数的所有值,其中 asfirst()
只为您提供第一个结果。您在搜索密钥时使用find()
和findOrFail()
。这就是我使用它们的方式:
When I want all data from a table I use the all() method
当我想要表中的所有数据时,我使用 all() 方法
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.
如果有一行的主键为 1,这将起作用。它应该只检索一行,所以我使用first()
而不是get()
. 请记住,如果您删除了使用键 1 的行,或者您的表中没有数据,您find(1)
将失败。
When I am looking for specific data as in a where clause:
当我在 where 子句中查找特定数据时:
Model::where('field', '=', 'value')->get();
When I want only the first value of the data in the where clause.
当我只想要 where 子句中数据的第一个值时。
Model::where('field', '=', 'value')->first();
回答by Gravy
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);
find
从数据库中返回一行并将其表示为 fluent / eloquent 对象。例如SELECT * FROM users WHERE id = 3
相当于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.
get
返回一个对象数组。例如SELECT * FROM users WHERE created_at > '2014-10-12'
,DB::table('users')->where('created_at', '>', '2014-10-12')->get()
将返回一个包含用户的对象数组,其中创建的字段比 4014-10-12 新。