何时在查询laravel 5中使用get() [英] when to use get() in query laravel 5
问题描述
我在laravel资源的show方法中设置了一个基本查询
I have a basic query set up in the show method of a laravel resource
public function show($id){
$results = Student::find($id);
$drives= Drive:: where('student_id', $id);
}
对$ results的查询非常有效.除非我在$ drives的末尾执行-> get(),否则该查询将不起作用.为什么是这样?这两个查询之间有什么区别,所以一个查询需要-> get()而另一个不需要?解决这个问题花了我大约5个小时的时间,我只是对它背后的功能感到好奇,所以我将来可以避免这种头痛.
The query for $results works perfectly. The query for $drives does not work unless I do ->get() at the end of it. Why is this? what's the difference between the two queries so that one requires the ->get() and the other does not? Solving this problem took me like 5 hrs and i'm just curious as to the functionality behind it so i can avoid this headache in the future.
推荐答案
某些eloquent
表达式隐式具有get
.那些由Query Builder
进行的通话将需要->get()
调用,而find(), findOne()...
则不需要get()
.
Some eloquent
expressions have a get
implicitly. Those ones who are made by a Query Builder
will need a ->get()
call, find(), findOne()...
won't need a get()
.
https://laravel.com/docs/5.6/eloquent#retrieving-models
https://laravel.com/docs/5.6/queries
这篇关于何时在查询laravel 5中使用get()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!