简体   繁体   English

laravel中的位置和查找之间有什么区别

[英]what is difference between where and find in laravel

when I write this $thread = Thread::find($id); 当我写这个$thread = Thread::find($id); then I write {{$thread->title}} it gives me the title of the thread, but when I write $thread = Thread::where('id','=',$id); 然后我写{{$thread->title}}它给了我线程的标题,但是当我写$thread = Thread::where('id','=',$id); then I write {{$thread->title}} it gives me an error.why is that happening? 然后我写了{{$thread->title}}这给了我一个错误。为什么会这样?

You should write: 您应该写:

$thread = Thread::where('id','=',$id)->first();

to get one column, else laravel will understand it as array. 得到一列,否则laravel会将其理解为数组。

You need to call the get() (or any of its variants) method to execute the actual query when using where. 使用where时,需要调用get()(或其任何变体)方法来执行实际查询。

   Thread::where('id','=',$id)->get();

Otherwise Thread::where('id','=',$id) just gets you an instance of eloquent's query builder. 否则Thread::where('id','=',$id)只会为您提供雄辩的查询生成器实例。

find() on the other hand will automatically run a query for whatever it is you want to find by you can't do all sorts of useful stuff (eg orderBy, paginate, etc.) that you can very easily pull of using the query builder. 另一方面, find()会自动运行一个查询,以查找您想要查找的内容,因为您无法做各种非常有用的事情(例如orderBy,paginate等),您可以很轻松地使用该查询建造者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM