Laravel自定义数据透视表

时间:2018-07-04 11:01:20

标签: php laravel

我想将第三个表链接到另外两个表。因此,我尝试使用自定义数据透视表失败。

我有三个表:

  • 公寓(我称之为“地段”)
  • 地板(我称之为“时代”)
  • 表面(我称其为“分数”)

我的关系:

  • 很多都有Etage(复式公寓)
  • 很多都有分数
  • 时代有很多
  • 年龄有很多分数
  • 分数有一个时代
  • 帮派有很多

现在我的模特:

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');
    }

}

但是每次我尝试在使用这些模型之一的网站上打开页面时,都会出现此错误:

  

App \ Models \ Copro \ Lot :: newPivot的声明(App \ Models \ Copro \ Eloquent $ parent,数组$ attribute,$ table,$ exists)应与Illuminate \ Database \ Eloquent \ Model :: newPivot兼容(Illuminate \ Database \ Eloquent \ Model $ parent,数组$ attributes,$ table,$ exists,$ using = NULL)

我找不到有用的东西来帮助我。我试图重写我的表,但效果不佳。

有人知道我为什么会出现此错误,以及例如如何使用此数据透视表获取公寓的所有分数?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您可以这样实现,而无需使用Pivot模型。

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');
    }

}

获取数据

$apartment = Lot::with('etageLots','etageLots.etage', 'fractions')->first();
dd($apartment->fractions);
foreach($apartment->etageLots as $etageLot){
    dd($etageLot->etage);
}

所有带分数的公寓

$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);
    }
}    
相关问题