【问题标题】:morphToMany real many to many relationmorphToMany 实多对多关系
【发布时间】:2021-04-05 02:41:43
【问题描述】:

我发现的所有多态示例都是一对多的,如果我理解正确的话。 (标签发布/视频,例如) 在我的情况下,父类是多个,子类也是。因此我设置了数据透视表

Tables

Person
  id

Venture
  id

Capital
  id

Estate
  id

PIVOT TABLE
Revenues
  emitter_id     //ID of the revenue emitting class (Venture, Capital, Estate)
  emitter_type   // Class of the Emitter (App\Models\Venture App\Models\Estate)
  receiver_id    // Id of Receiver (Venture or Person)
  receiver_type  // type of Receiver (App\Models\Venture or App\Models\Person)
  revenue

在房地产模型中我试试这个

    public function revenuePersons()
    {
   //                           searched type,  own type/id  ,tabel      own ID       to search id
        return $this->morphToMany(Person::class, 'emitter' ,'revenues' ,'emitter_id','receiver_id')
        ->withPivot('revenue');
    }

一个人模型

     public function estaterevenues(){
        //                       searched type,  own type/id  ,tabel      own ID       to search id             
         return $this->morphToMany(Estate::class, 'receiver' ,'revenues' ,'receiver_id','emitter_id')
         ->withPivot('revenue');        
     }

代码有效,但在某些情况下,我得到了额外的关系。所以它接缝搜索的_type没有被正确考虑。

所以我开始实现一个自己的数据库查询函数,它可以返回收入条目。它工作正常。

Revenue Model
 public function getRevenue($ownside, $emitter_id = Null, $emitter_type,$receiver_id=Null, $receiver_type ){
$revenue = DB::table('revenues')
    ->where('emitter_id', $emitter_id)
     .....()->get()}

但我不能做类似的事情

$persons->getRevenues

因为关系应该作为返回值

因此,如果有人知道如何正确地做到这一点,我会非常高兴。或者这种多对多方法的其他一些最佳实践。

第二个问题是如何一次获得所有收入接收者。 而不是

$estate->revenuepersons
$estate->revenueventures

有类似的东西

$estate->revenues  //that list both, Ventures and Persons

这里是一个类图

【问题讨论】:

    标签: php laravel eloquent


    【解决方案1】:
    
    namespace App;
     
    use Illuminate\Database\Eloquent\Model;
     
    class Post extends Model
    {
        /**
         * Get all of the tags for the post.
         */
        public function tags()
        {
            return $this->morphToMany(Tag::class, 'taggable');
        }
    }
    
    
    namespace App;
     
    use Illuminate\Database\Eloquent\Model;
     
    class Video extends Model
    {
        /**
         * Get all of the tags for the post.
         */
        public function tags()
        {
            return $this->morphToMany(Tag::class, 'taggable');
        }
    }
    
    namespace App;
     
    use Illuminate\Database\Eloquent\Model;
     
    class Tag extends Model
    {
        /**
         * Get all of the posts that are assigned this tag.
         */
        public function posts()
        {
            return $this->morphedByMany(Post::class, 'taggable');
        }
     
        /**
         * Get all of the videos that are assigned this tag.
         */
        public function videos()
        {
            return $this->morphedByMany(Video::class, 'taggable');
        }
    }
    
    $post = Post::find(1);  
     
    dd($post->tags);
    $video = Video::find(1);    
     
    dd($video->tags);
    
    
    $tag = Tag::find(1);    
     
    dd($tag->posts);
    $tag = Tag::find(1);    
     
    dd($tag->videos);
    
    posts table migration:
    
    Schema::create('posts', function (Blueprint $table) {
    
        $table->increments('id');
    
        $table->string("name");
    
        $table->timestamps();
    
    });
    
    videos table migration:
    
    Schema::create('videos', function (Blueprint $table) {
    
        $table->increments('id');
    
        $table->string("name");
    
        $table->timestamps();
    
    });
    
    tags table migration:
    
    Schema::create('tags', function (Blueprint $table) {
    
        $table->increments('id');
    
        $table->string("name");
    
        $table->timestamps();
    
    });
    
    taggables table migration:
    
    Schema::create('taggables', function (Blueprint $table) {
    
        $table->integer("tag_id");
    
        $table->integer("taggable_id");
    
        $table->string("taggable_type");
    
    });
    

    【讨论】:

    • HI Darshan,感谢您的评论,我已经知道标签视频发布示例。当然,它工作正常。但我在“标签方面”也有两种课程。我在第一篇文章中附上了一张图表。因此,我添加了一个“tag_type”属性。但是没有得到它来查询正确的结果。
    猜你喜欢
    • 2020-08-22
    • 1970-01-01
    • 1970-01-01
    • 2013-04-25
    • 2020-06-26
    • 2012-06-05
    • 2018-11-29
    • 2017-01-28
    • 2021-05-25
    相关资源
    最近更新 更多