如何在 Laravel Eloquent 中进行以下查询?
SELECT catID, catName, imgPath FROM categories WHERE catType = "Root"
我试过跟随
CategoryModel::where('catType', '=', 'Root')
->lists('catName', 'catID', 'imgPath');
但它只返回两个字段。
Array ( [7] => Category 1 )
最佳答案
lists()
将生成的集合转换为具有键值的数组。那里只能有两个数据库列。否则你必须使用 select()
但是你会得到一个模型集合而不仅仅是一个数组。
$categories = CategoryModel::select('catID', 'catName', 'imgPath')
->where('catType', '=', 'Root')
->get();
关于php - 如何在 Laravel Eloquent 中选择某些字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28530364/