How to Select Certain Fields in Laravel Eloquent?

后端 未结 5 899
野性不改
野性不改 2021-01-04 10:09

How to Do following Query in Laravel Eloquent?

SELECT catID, catName, imgPath FROM categories WHERE catType = \"Root\"

I have tried followi

5条回答
  •  春和景丽
    2021-01-04 10:37

    lists() turns the resulting collection into an array with key value. You can only have two database columns in there. Otherwise you have to use select() but then you will get a collection of models not just an array.

    $categories = CategoryModel::select('catID', 'catName', 'imgPath')
                               ->where('catType', '=', 'Root')
                               ->get();
    

提交回复
热议问题