0
我想使用如自定义透视型号:Laravel定制透视模型中缺失领域
class A extends Model{
public function b()
{
return $this->belongsToMany(B::class)
->using(PivotAB::class);
}
class PivotAB extends Pivot{}
当从数据透视表访问PivotAB
通过关系附加字段缺少(从artisan tinker
输出):
>>>$q = A::all();
=> Illuminate\Database\Eloquent\Collection {#1385
all: [
App\Models\A {#1386
id: 1
},
],
}
>>> $q[0]->b[0]->pivot;
=> App\Models\PivotAB {#1389
a_id: 1,
b_id: 1,
}
>>> $q[0]->b[0]->pivot->custom_field;
=> null
但是,当我查询枢轴模式可以直接现场被填充:
>>> PivotAB::all();
=> Illuminate\Database\Eloquent\Collection {#1382
all: [
App\Models\PivotAB{#281
a_id: 1,
b_id: 1,
custom_field: "abc",
},
],
}
我错过了什么?我是否需要在某处声明枢轴字段?