Laravel-何时使用-> get() [英] Laravel - When to use ->get()

查看:335
本文介绍了Laravel-何时使用-> get()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Laravel中->get()的时间感到困惑...

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

E.G. DB::table('users')->find(1)不需要-> get()来检索结果,User::find(1)

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

laravel文档说"...使用get或first方法执行查询..."

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

我已经阅读了Fluent Query Builder和Eloquent文档,但不了解何时需要使用get()...

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

感谢您的帮助

推荐答案

由于find()函数将始终使用表的主键,因此不需要get().因为您无法缩小选择范围,所以这就是为什么它总是尝试获取该记录并将其返回.

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.

但是当您使用Fluent Query Builder时,您可以像这样嵌套条件:

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

$userQuery = DB::table('users');
$userQuery->where('email', '=', 'foo@bar.com');
$userQuery->or_where('email', '=', 'bar@foo.com');

这允许您在整个代码中添加条件,直到您真正想要获取它们为止,然后您将调用get()函数.

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();

这篇关于Laravel-何时使用-> get()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆