I have the following models and relationships:
class Fence extends Model {
public function fenceLines(){
return $this->hasMany('App\Models\FenceLine');
}
public function newPivot(Model $parent, array $attributes, $table, $exists){
if ($parent instanceof FenceLine) {
return new FenceLine($parent, $attributes, $table, $exists);
}
return parent::newPivot($parent, $attributes, $table, $exists);
}
}
class FencePoint extends Model {
public function fenceLines(){
return $this->hasMany('App\Models\FenceLine');
}
public function newPivot(Model $parent, array $attributes, $table, $exists){
if ($parent instanceof FenceLine) {
return new FenceLine($parent, $attributes, $table, $exists);
}
return parent::newPivot($parent, $attributes, $table, $exists);
}
}
class FenceLine extends Pivot {
protected $table = 'fences_fence_points';
public function fence(){
return $this->belongsTo('App\Models\Fence');
}
public function fencePoint(){
return $this->belongsTo('App\Models\FencePoint');
}
}
when I call $fence->fenceLines()
I get the following error:
Argument 1 passed to App\Models\Fence::newPivot() must be an
instance of Illuminate\Database\Eloquent\Model, none given
I have read many blogs about this exact issue and I couldn't find any with the solution.
I FINALLY found it.
There were 2 issues.
Such as:
such as in Fence Model:
In FencePoint Model:
You can then use the pivot model such as: