Reputation: 463
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 :
My relationship :
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.
Upvotes: 0
Views: 1289
Reputation: 8307
You can achieve it like this, without using Pivot model.
class Lot extends Model
{
public function etageLots(){
return $this->hasMany('App\Models\Copro\EtageLot');
}
public function fractions()
{
return $this->hasManyThrough('App\Models\Copro\Fraction','App\Models\Copro\EtageLot', 'etage_id', 'etage_lot_id');
}
}
class Etage extends Model
{
public function lotEtages()
{
return $this->hasMany('App\Models\Copro\EtageLot');
}
}
class EtageLot extends Model
{
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('App\Models\Copro\EtageLot','etage_lot_id');
}
}
Fetch Data
$apartment = Lot::with('etageLots','etageLots.etage', 'fractions')->first();
dd($apartment->fractions);
foreach($apartment->etageLots as $etageLot){
dd($etageLot->etage);
}
All apartment with fraction counts
$apartments = Lot::with('etageLots','etageLots.etage', 'fractions')->withCount('fractions')->get();
foreach($apartments as $apartment){
dd($apartment->fractions_count);
dd($apartment->fractions); //direct fractions for each apartment
foreach($apartment->etageLots as $etageLot){
dd($etageLot->etage);
}
}
Upvotes: 1