多对多(多态)使用同一个模型不同类型

我在数据库中有这 3 个表:

http://img1.mukewang.com/61d15a440001f13c06490191.jpg

我正在使用多对多(多态)雄辩关系来连接模型。的问题是,所述Creadores表可以是类型artista或autor在Creaciones表中。是否可以告诉 Eloquent 何时使用artista或autor?


如果我将Creador模型扩展到其他 2 个模型,它会起作用:Artista和Autor. 但是,当我想显示所有creacionesA的creador使用Creador模式,因为多态关系与扩展模型创建是不可能的。


Libro 型号:


<?php


namespace App;


use Illuminate\Database\Eloquent\Model;

use ChrisKonnertz\BBCode\BBCode;


class Libro extends Model

{

    protected $table = 'Libros';


    // Return all the artists of the book

    public function artistas()

    {

        return $this->morphedByMany('App\Creador', 'creador', 'creaciones');

    }


    // Return all the authors of the book

    public function autores()

    {

        return $this->morphedByMany('App\Creador', 'creador', 'creaciones');

    }

}

克雷多型号:


<?php


namespace App;


use Illuminate\Database\Eloquent\Model;


class Creador extends Model

{

    protected $table = 'creators';


    // Return all the books where author

    public function autorLibros()

    {

        return $this->morphToMany('App\Libro', 'creador', 'creaciones');

    }


    // Return all the books where artist

    public function artistaLibros()

    {

        return $this->morphToMany('App\Libro', 'creador', 'creaciones');

    }

}


ITMISS
浏览 165回答 2
2回答

慕后森

您最好在其中添加一个type属性Creadorwith 'artista'/ 'autor'。多态关系只能采用单一模型。所以你的代码会变成:public function creadors(){&nbsp; &nbsp; // Return a general relation for all 'creadores'.&nbsp; &nbsp; return $this->morphedByMany(App\Creador::class, 'creador', 'creaciones');}public function artistas(){&nbsp; &nbsp; // Filter for 'artista's.&nbsp; &nbsp; return $this->creadors()->where('type', 'artista');}public function autores(){&nbsp; &nbsp; // Filter for 'autor's.&nbsp; &nbsp; return $this->creadors()->where('type', 'autor');}

阿晨1998

用下面的方法解决了。将关系从多态多对多更改为普通多对多,添加withPivot和wherePivot。克里多尔模型<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class Creador extends Model{&nbsp; &nbsp; protected $table = 'creators';&nbsp; &nbsp; public function libros()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->belongsToMany('App\Libro', 'creaciones')->withPivot('creador_type');&nbsp; &nbsp; }&nbsp; &nbsp; // All books as an Artist&nbsp; &nbsp; public function librosArtista()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->libros()->wherePivot('creador_type', 1);&nbsp; &nbsp; }&nbsp; &nbsp; // All books as an Author&nbsp; &nbsp; public function librosAutor()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->libros()->wherePivot('creador_type', 2);&nbsp; &nbsp; }}Libro 模型<?phpnamespace App;use Illuminate\Database\Eloquent\Model;use ChrisKonnertz\BBCode\BBCode;class Libro extends Model{&nbsp; &nbsp; protected $table = 'libros';&nbsp; &nbsp; public function creador()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->belongsToMany('App\Creador', 'creaciones')->withPivot('creador_type');&nbsp; &nbsp; }&nbsp; &nbsp; // All book artists&nbsp; &nbsp; public function artistas()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->creador()->wherePivot('creador_type', 1);&nbsp; &nbsp; }&nbsp; &nbsp; // All book authors&nbsp; &nbsp; public function autores()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->creador()->wherePivot('creador_type', 2);&nbsp; &nbsp; }}当创建一个附加Creador到 a 时Libro:$libro->artistas()->attach( $creador, [&nbsp; &nbsp; 'creador_type' => 1]);
打开App,查看更多内容
随时随地看视频慕课网APP