I want to link a third table to two others. So I tried to use a custom pivot table without success.
I have three tables :
- Apartment (I call it "Lot")
- Floor (I call it "Etage)
- Surface (I call it "Fraction")
My relationship :
- Lot has many Etage (duplex apartment)
- Lot has many Fraction
- Etage has many Lot
- Etage has many Fraction
- Fraction has one Etage
- Fraction has one Lot
And my models right now :
class Lot extends Model
{
public function etages(){
return $this->belongsToMany('App\Models\Copro\Etage');
}
public function newPivot(Eloquent $parent, array $attributes, $table, $exists) {
if ($parent instanceof Event) {
return new EtageLot($parent, $attributes, $table, $exists);
}
return parent::newPivot($parent, $attributes, $table, $exists);
}
}
class Etage extends Model
{
public function lots()
{
return $this->belongsToMany('App\Models\Copro\Lot');
}
public function newPivot(Eloquent $parent, array $attributes, $table, $exists) {
if ($parent instanceof User) {
return new EtageLot($parent, $attributes, $table, $exists);
}
return parent::newPivot($parent, $attributes, $table, $exists);
}
}
class EtageLot extends Pivot
{
protected $table = 'etage_lot';
public function lot(){
return $this->belongsTo('App\Models\Copro\Lot');
}
public function etage(){
return $this->belongsTo('App\Models\Copro\Etage');
}
public function fractions()
{
return $this->hasMany('App\Models\Copro\Fraction','etage_lot_id');
}
}
class Fraction extends Model
{
public function type(){
return $this->belongsTo('App\Models\Copro\Type');
}
public function EtageLot(){
return $this->belongsTo('etage_lot','etage_lot_id');
}
}
But each time I try to open a page on my site that use one of these models, I have this error :
Declaration of App\Models\Copro\Lot::newPivot(App\Models\Copro\Eloquent $parent, array $attributes, $table, $exists) should be compatible with Illuminate\Database\Eloquent\Model::newPivot(Illuminate\Database\Eloquent\Model $parent, array $attributes, $table, $exists, $using = NULL)
I don't find something usefull to help me. I tried to rewrite my tables but it doesn't work better.
Someone know why I have this error and how to use this pivot table to get all fractions for an apartment for example ?
Thank for your help.