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",
},
],
}
我錯過了什麼?我是否需要在某處聲明樞軸字段?