【问题标题】:Many To Many (Polymorphic) using the same model with different types多对多(多态)使用具有不同类型的相同模型
【发布时间】:2020-01-08 21:49:13
【问题描述】:

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

我正在使用多对多(多态)雄辩关系来连接模型。问题是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


    【解决方案1】:

    通过以下方式解决。将关系从多态多对多更改为普通多对多,添加withPivot 和wherePivot。

    创造者模型

    <?php
    
    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Creador extends Model
    {
        protected $table = 'creators';
    
        public function libros()
        {
            return $this->belongsToMany('App\Libro', 'creaciones')->withPivot('creador_type');
        }
    
        // All books as an Artist
        public function librosArtista()
        {
            return $this->libros()->wherePivot('creador_type', 1);
        }
    
        // All books as an Author
        public function librosAutor()
        {
            return $this->libros()->wherePivot('creador_type', 2);
        }
    }
    

    图书馆模型

    <?php
    
    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    use ChrisKonnertz\BBCode\BBCode;
    
    class Libro extends Model
    {
        protected $table = 'libros';
    
        public function creador()
        {
            return $this->belongsToMany('App\Creador', 'creaciones')->withPivot('creador_type');
        }
    
        // All book artists
        public function artistas()
        {
            return $this->creador()->wherePivot('creador_type', 1);
        }
    
        // All book authors
        public function autores()
        {
            return $this->creador()->wherePivot('creador_type', 2);
        }
    }
    

    当创建一个将Creador 附加到Libro 时:

    $libro->artistas()->attach( $creador, [
        'creador_type' => 1
    ]);
    

    【讨论】:

      【解决方案2】:

      您最好将type 属性添加到Creador 并在其中包含'artista'/'autor'。

      多态关系只能取单个模型。 所以你的代码会变成:

      public function creadors()
      {
          // Return a general relation for all 'creadores'.
          return $this->morphedByMany(App\Creador::class, 'creador', 'creaciones');
      }
      
      public function artistas()
      {
          // Filter for 'artista's.
          return $this->creadors()->where('type', 'artista');
      }
      
      public function autores()
      {
          // Filter for 'autor's.
          return $this->creadors()->where('type', 'autor');
      }
      

      【讨论】:

      • 如果我添加一个 type 属性,当使用 attach() 关联模型时我将如何添加它? $libro-&gt;autores()-&gt;attach( $creador );
      • 在该关系上调用 attach 不会对 type 执行任何操作,因此您必须为该 $creador 实例手动设置 type 属性。
      猜你喜欢
      • 1970-01-01
      • 2020-05-14
      • 2017-07-10
      • 1970-01-01
      • 1970-01-01
      • 2011-08-05
      • 2016-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多