Laravel 5 hasManyThrough

后端 未结 2 836
礼貌的吻别
礼貌的吻别 2021-02-06 02:31

I have 3 tables: company <-> users <-> invoice.

A company hasMany users.

A user belongsTo

相关标签:
2条回答
  • 2021-02-06 02:58

    You can also do this in the Course class, so when you use ->with('chapters'), it automatically loads the videos too:

    public function chapters()
    {
        return $this->hasMany('Moubarmij\Models\Chapter')->with('videos');
    }
    
    0 讨论(0)
  • 2021-02-06 03:04

    Let's say we have table A and B and C where table A has many of B (OneToMany) and B has many of C (OneToMany) inorder to access the table C from table A you can use the Laravel shortcut (HasManyThrough) on the Table A and the problem is solved

    BUT If you have table A and B and C where table A has many of B (OneToMany) and B has many of C (ManyToMany) you cannot use the laravel's (HasManyThrough) shortcut to access the table C from table A, {because of the pivot table in the middle between B and C} what you can do in this case is very simple:

    In this example table A will be [courses], table B will be [chapters], and table C will be [videos] where every course has may chapters, while a chapter can belong to only one course. in the other hand every chapter has many videos while a video can belong to many chapters.

    <?php namespace Moubarmij\Models;
    
    use Eloquent;
    
    class Video extends Eloquent{
    
       protected $table = 'videos';
    
        /*************************************************************
         * Query Scopes
         **************************************************************/
    
        public function scopePublished($query)
        {
            return $query->where('published', '=', '1');
        }
    
        public function scopeOrdered($query)
        {
            return $query->orderBy('order_number', 'ASC');
        }
    
        /*************************************************************
         * Relations
         **************************************************************/
    
        public function chapters()
        {
            return $this->belongsToMany('Moubarmij\Models\Chapter', 'chapters_videos');
        }
    
    
    }
    

    <?php namespace Moubarmij\Models;
    
    use Eloquent;
    
    class Chapter extends Eloquent{
    
       protected $table = 'chapters';
    
        /*************************************************************
         * Query Scopes
         **************************************************************/
    
        public function scopePublished($query)
        {
            return $query->where('published', '=', '1');
        }
    
        public function scopeOrdered($query)
        {
            return $query->orderBy('order_number', 'ASC');
        }
    
        public function scopeWithVideos($query)
        {
            return $query->with(['videos' => function($q)
            {
                $q->ordered();
            }]);
        }
    
    
        /*************************************************************
         * Relations
         **************************************************************/
    
        public function course()
        {
            return $this->belongsTo('Course');
        }
    
        public function videos()
        {
            return $this->belongsToMany('Moubarmij\Models\Video', 'chapters_videos');
        }
    
    }
    

    <?php namespace Moubarmij\Models;
    
    use Eloquent;
    
    class Course extends Eloquent{
    
       protected $table = 'courses';
    
        /*************************************************************
         * Query Scopes
         **************************************************************/
    
        public function scopeVisible($query)
        {
            return $query->where('visible', '=', '1');
        }
    
        public function scopeOrdered($query)
        {
            return $query->orderBy('order_number', 'ASC');
        }
    
        public function scopeWithChapters($query)
        {
            return $query->with(['chapters' => function($q)
            {
                $q->ordered();
            }]);
        }
    
        public function scopeWithChaptersAndVideos($query)
        {
            return $query->with(['chapters' => function($q)
            {
                $q->ordered()->withVideos();
            }]);
        }
    
    
        /*************************************************************
         * Relations
         **************************************************************/
    
        public function chapters()
        {
            return $this->hasMany('Moubarmij\Models\Chapter');
        }
    
    
    }
    
    0 讨论(0)
提交回复
热议问题