Visit Model
id
user_id
visit_date
public function user()
{
return $this->belongsTo(USER::class,'user_id','user_id')->withTrashed();
}
User Model
user_id
branch_id
name
public function branch()
{
return $this->belongsTo(BRANCH::class,'branch_id','branch_id')->withTrashed();
}
Branch Model
branch_id
branch_desc
I want to display branch_desc when getting list of Visit.
Currently Im showing only branch_id from User Model
4
Answers
If figured it out.
Use this in getting list of your model in Controller.
This is how to query with multiple relationship
You need to make one to Many relation from visit to user table and user to branch table
ex. of relationship code:
This will allow you to eager load the relationships in you controller like this
I will recommend you to take a look at laravel relationships docs for a better understanding
Laravel Relationships with eager loading
For 2 layers (or more) relationships, I recommend you install this package from Staudenmeir https://github.com/staudenmeir/eloquent-has-many-deep
Inside your Visit model, you can add the relationship like this:
And for more information , check LaravelDaily tutorial related to this package https://youtu.be/wgdWokrm3Mw
Your question isn’t clear really! However from what I understand from your question you should also have a Branch model and migration.
So, from visit table you will fetch the users by the relation with user in your visit model.
Then when you can fetch your user branches from your User model by your users relation with Branch.
So, you will fetch data like this:
You can replace description with any column_name_that_wish_to_fetch.