【发布时间】:2020-01-08 21:49:13
【问题描述】:
我正在使用多对多(多态)雄辩关系来连接模型。问题是Creadores 表可以是Creaciones 表中的artista 或autor 类型。
是否可以告诉 Eloquent 何时使用 artista 或 autor?
如果我将 Creador 模型扩展为 2 个其他模型:Artista 和 Autor,它将起作用。但是,当我想使用Creador 模型显示creador 的所有creaciones 时,这是不可能的,因为多态关系是使用扩展模型创建的。
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');
}
}
【问题讨论】:
标签: php laravel eloquent many-to-many