How to Select Certain Fields in Laravel Eloquent?

后端 未结 5 920
野性不改
野性不改 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:36

    If you want to get certain columns then You can use one of the two method get() or all()

    but the syntax is different for both, get() method takes array as argument and all() method takes string or array both as argument:

    Model::all('field1','field2') with string as arguments

    CategoryModel::all('catName', 'catID', 'imgPath')->where('catType','Root');
    

    Model::all(['field1','field2']) with array as arguments

    CategoryModel::all(['catName', 'catID', 'imgPath'])->where('catType','Root');
    

    Model::get(['field1','field2'])

    CategoryModel::get(['catName', 'catID', 'imgPath'])->where('catType','Root');
    

提交回复
热议问题