根据字段的 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();
求大神指点!!再此先谢过
推荐文章: