我正在对模型进行局部作用域。在我的研究范围之内,我正在加入表格,但是在我的结果集上,我却失去了对我的人际关系的参考。
在入门模型中,我有 这种关系:
public function items() {
return $this->hasMany('App\Models\Accounting\EntryItem', 'entry_id');
}
以及此范围:
public function scopeWithConst($query, $const_code) {
return $query->join('consts', function($join) use($const_code) {
$join->on('consts.code', '=', $const_code);
});
}
在控制器中:
$entry = Entry::with('items')->withConst('entries.status')->find($id);
由于调用“ withConst”作用域,它总是返回“ items”:[]!
有什么解释吗?
答案 0 :(得分:0)
您将必须在要查找的条目中用该特定$ id的'entries.status'值替换'entries.status'。您可以通过闭包或在调用“ withConst”之前找到值来实现。
System.exit(0)
或
$status = Entry::find($id)->pluck('status');
$entry = Entry::with('items')->withConst($status)->find($id);
据我所知,作用域是可重用的约束,不是为涉及2个表的联接设计的。根据您需要多少示波器,以无示波器的常规方式进行保存。