根据字段的 type 的不同,关联不同的模型该怎么写
如果表 A type 值 是 1 就关联 B 表 如果 A 的 type 是 2 则关联 C 表
public function test()
{
if($this->type == 1) {
return $this->hasOne(A::class, 'order_id', 'order_no');
} else {
return $this->hasOne(B::class, 'order_id', 'order_no');
}
}
我照上面这样写不行
dd($this->id) 的值为 null
控制器里是这样写的
$data = A::with('test')->get();
求大神指点!!再此先谢过
推荐文章: