我在数据库中有这 3 个表:
我正在使用多对多(多态)雄辩关系来连接模型。的问题是,所述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');
}
}
慕后森
阿晨1998