在laravel中使用with實現(xiàn)動態(tài)添加where條件
關(guān)鍵點:閉包
模型:
public function getCollect() { return $this->belongsTo('App\Components\Misc\Models\CollectCareerTalk', 'id', 'career_talk_id'); } public function otherMethod() { return $this->belongsTo('App\Components\Misc\Models\OtherMethodModel', '主鍵', '外鍵'); }
倉庫:
$this->model->with(['getCollect' => function ($q) use ($user_id) { $q->where('user_id', $user_id); }, 'otherMethod']) ->select('id', 'title') ->where([ 'id' => 1 ]) ->first();
注意:with可以連多個表(數(shù)組形式傳參),沒有動態(tài)條件的,可以直接講模型方法名寫到with的參數(shù)中,有動態(tài)條件的,寫到閉包中
以上這篇在laravel中使用with實現(xiàn)動態(tài)添加where條件就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
thinkphp3.x自定義Action、Model及View的簡單實現(xiàn)方法
這篇文章主要介紹了thinkphp3.x自定義Action、Model及View的簡單實現(xiàn)方法,結(jié)合實例形式詳細分析了thinkPHP3.x自定義模型、視圖及控制器的具體步驟與相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2016-05-05php float不四舍五入截取浮點型字符串方法總結(jié)
在php中截取浮點型大致有以下幾種方法。需要的朋友可以過來參考下,希望對大家有所幫助2013-10-10laravel數(shù)據(jù)庫查詢結(jié)果自動轉(zhuǎn)數(shù)組修改實例
這篇文章主要介紹了laravel數(shù)據(jù)庫查詢結(jié)果自動轉(zhuǎn)數(shù)組修改實例,有需要的同學可以借鑒參考下2021-02-02Laravel框架運行出錯提示RuntimeException No application encryption ke
這篇文章主要介紹了Laravel框架運行出錯提示RuntimeException No application encryption key has been specified.解決方法,涉及Laravel框架相關(guān)配置、設(shè)置與運行操作技巧,需要的朋友可以參考下2019-04-04