在laravel中使用with實現動態(tài)添加where條件
更新時間:2019年10月10日 10:29:04 作者:攻城獅阿楠
今天小編就為大家分享一篇在laravel中使用with實現動態(tài)添加where條件,具有好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
關鍵點:閉包
模型:
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可以連多個表(數組形式傳參),沒有動態(tài)條件的,可以直接講模型方法名寫到with的參數中,有動態(tài)條件的,寫到閉包中
以上這篇在laravel中使用with實現動態(tài)添加where條件就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
thinkphp3.x自定義Action、Model及View的簡單實現方法
這篇文章主要介紹了thinkphp3.x自定義Action、Model及View的簡單實現方法,結合實例形式詳細分析了thinkPHP3.x自定義模型、視圖及控制器的具體步驟與相關實現技巧,需要的朋友可以參考下2016-05-05Laravel框架運行出錯提示RuntimeException No application encryption ke
這篇文章主要介紹了Laravel框架運行出錯提示RuntimeException No application encryption key has been specified.解決方法,涉及Laravel框架相關配置、設置與運行操作技巧,需要的朋友可以參考下2019-04-04