Laravel: Get Object From Collection By Attribute

前端 未结 9 1715
暖寄归人
暖寄归人 2020-12-23 02:35

In Laravel, if I perform a query:

$foods = Food::where(...)->get();

...then $foods is an Illuminate Collection of Foo

9条回答
  •  感动是毒
    2020-12-23 03:30

    As the question above when you are using the where clause you also need to use the get Or first method to get the result.

    /**
    *Get all food
    *
    */
    
    $foods = Food::all();
    
    /**
    *Get green food 
    *
    */
    
    $green_foods = Food::where('color', 'green')->get();
    

提交回复
热议问题