For getting every new tutorial link Please join our telegram group

Part 5 laravel eloquent hasManyThrow() relationship with example with hindi video

This laravel tutorial about hasManyThrow() eloquent relationship with a video in Hindi and explain with source code.

Also ask laravel hasManyThrow() relationship not working, example of hasManyThrow relationship, laravel eloquent hasManyThrow relationship tutorial and laravel relationship to connect to tables or models which are not connected directly.

Laravel hasManyThrow() relationship with example

Laravel hasManyThrow() relationship use to connect two model that are not connected directly they connect by a third model. has-many-throw relationship work same as hasOneThrow() relationship but only difference between them is hasOneThrow connected all tables by one to one relationship and hasManyThrow connected by one to many relationship.

hasManyThrow() relationship in simple word table A connected to table B by hasMany/hasOne and table B connected to table C by hasMany/hasOne than we define A hasManyThrough C.


Tables
channels => id, name
videos => id, channel_id, title
comments => id, video_id, content

Let explain an example of hasManyThrough() relationship include above three tables, channels table not connected directly to comments table but connected to videos and videos table connected to comments by has many relationship, thus we define directly channelComments method for hasManyThrough() relationship in Channel model.

Order model
<?

namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class Channel extends Model {
    
    public function channelComments() {
        return $this->hasManyThrough(Comment::class, Video::class);
    }
}

Now we get chennel all comments in controller by model channelComments() method.

ChannelController
<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Channel;

class ChannelController extends Controller{
    public function index(){
       $Channels = Channel::with('channelComments')->get();

	dd($Channels);
    }
}

hasManyThrough() relationship customize when keys name changed

When we customize our table primary key and foreign key column name then pass both foreign keys in third and fourth argument and local keys in fifth and sixth argument see blow model code example with customizing key names.

<?php

namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class Channel extends Model {
    
    public function channelComments() {
        return $this->hasOneThrough(
        	Comment::class,
        	Video::class,
        	'channel_id', // Foreign key in the Video table
        	'video_id', // Foreign key on the Comment table
         	'id', // Local key on the channels table
        	'id', // Local key on the comments table
        );
    }
}

You read this laravel tutorial on advanced web tutorial. here we provide laravel beginners to advanced tutorial in hindi video.

php laravel developer anmol sharma

Anmol Sharma

I am a software engineer and have experience in web development technologies like php, Laravel, Codeigniter, javascript, jquery, bootstrap and I like to share my deep knowledge by these blogs.

Related tutorial links