Ich versuche, eine benutzerdefinierte Pivot Modell wie zu verwenden:Laravel Benutzerdefinierte Pivot Modell fehlt Felder
class A extends Model{
public function b()
{
return $this->belongsToMany(B::class)
->using(PivotAB::class);
}
class PivotAB extends Pivot{}
Beim Zugriff auf PivotAB
durch die Beziehung das zusätzliche Feld aus der Pivot-Tabelle ist (Ausgabe von artisan tinker
) fehlt:
>>>$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
Aber wenn ich abfragen, um das Pivot-Modell direkt das Feld aufgefüllt wird:
>>> PivotAB::all();
=> Illuminate\Database\Eloquent\Collection {#1382
all: [
App\Models\PivotAB{#281
a_id: 1,
b_id: 1,
custom_field: "abc",
},
],
}
Was fehlt mir? Muss ich die Pivot-Felder irgendwo deklarieren?