本文介紹了如何在 Laravel Eloquent 中選擇某些字段?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
如何在 Laravel Eloquent 中執行以下查詢?
How to Do following Query in 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()
但是你會得到一個模型集合而不僅僅是一個數組.
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();
這篇關于如何在 Laravel Eloquent 中選擇某些字段?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!