Laravel 5 hasManyThrough

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

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

A company hasMany users.

A user belongsTo

2条回答
  •  别那么骄傲
    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.

    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');
        }
    
    
    }
    

    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');
        }
    
    }
    

    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');
        }
    
    
    }
    

提交回复
热议问题