Laravel 中預加載指定查詢字段


直接上代碼

$result = User::with('topics:user_id,body,title') ->where('name', 'Summer') ->get() ->toArray(); 

如果要進行約束預加載,應該這樣寫。

$result = User::with(['topics' => function ($query) { $query->select('id', 'user_id', 'body', 'title') ->orderBy('updated_at', 'desc'); }]) ->where('name', 'Summer') ->get() ->toArray(); 

在使用預加載時,表的關聯字段一定要查詢,否則查詢后的數據沒法匹配

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM