I have done a lot of research in Google about this. There are not a good examples with using this type of relation between models.
Can somebody share own sample using Has-Many-Through Relations in Laravel?
Let's take a sample of a social network where you have User, their Friends and their friends' Photos. 3 models. yep a friend is just a normal user but for this example, a Friend would be a separate Model.
1- A user has friends
public function friends() {
return $this->hasMany(Friend::class);
}
2 - A friend has photos
public function photos() {
return $this->hasMany(Photo::class);
}
3 - You are the admin. you have many users, including John. John has a lot a friends. So sometimes, you want to see John's friends photos, maybe those where they tag him. So in user you define the relationship
public function friendsPhotos() {
return $this->hasManyThrough(Friend::class, Photo::class); //could add a scope here for picture with tags only.
}
Not problably the best example. But it shows how you can see all John's friends pictures by just doing
$john = User::where('email', 'john#email.com')->first();
$john->friendsPhotos();
If the example doesnt suit you well, now think of a bank looking for their best salesperson in a specific Branch. A Branch hasMany Salesperson, and each Salesperson hasMany Customers they deal with.
In Branch model, you define
public function customers() {
return $this->hasManyThrough(Salesperson::class, Customer::class);
}
I hope this helps a little bit.
Database tables for the banking system
1- Branch
id
name
...
2 - Salesperson
id
branch_id
firstname
...
3 - Customer
id
salesperson_id
firstname
...
Related
I've one many to many relationship between Entities and Affiliated Tables,
basically an affiliated can attend to many entities(example, medical services, dentist offices). One Entity can be visited by one or many affiliated.
Additionaly , one or more benefits can be occupied by one affiliated in a particular entity. As shown in the image.
I want to be able to answer queries like, which benefits where used by the affiliated in a particular entity.
Should i create a new Model, how should i rewrite the many-to-many relationship for ORM.
Thanks in advance!
Don't need a Affiliated_has_Entities model.
You could write the relationships as below, remember to check for arguments and its order in the belognsToMany relationship (sorry for misspelled words)
Entities model
public function afilliateds(){
return $this->belongsToMany('Afilliated', 'Afilliated_has_Entities');
}
Afilliated model
public function entities(){
return $this->belongsToMany('Entities', 'Afilliated_has_Entities');
}
public function benefits(){
return $this->belongsToMany('Entities', 'Afilliated_has_Entities');
}
Benefits model
public function entites(){
return $this->belongsToMany('Entities', 'Afilliated_has_Entities');
}
And so on..
I hope it helps
I have tables:
user
id
name
companies
id
name
company_user
company_id
user_id
Tables has Many To Many relationships.
As it complicated relationship for me, I can't find way how to make this limit, when user can see companies that was created by this user. (probably not well experienced)
Now I have this, but user can see any company
CompanyController:
public function show($company_id)
{
$company = Company::where('id', $company_id)->firstOrFail();
return view('company.settings', compact('company'));
}
So tip me please how to make user can see only companies created by this user.
You can do this:
public function show($company_id)
{
$company = auth()->user()->companies()->findOrFail($company_id);
return view('company.settings', compact('company'));
}
It will scope the company to the currently logged in user (through the many-to-many relationship on the User model). If none is found, it will return 404.
Since it many to many relation, you can map one company to many users, also map one user to many companies. Check if you have not mistakenly assign the company to many user
Also the above code can be written the way
public function show($company_id)
{
$company = Company::findOrFail($company_id);
return view('company.settings', compact('company'));
}
I am working in laravel5.4. I have created four table as ticket, users, and company. Here, I have stored users id in ticket table. And stored company id in users table.
Here, I want to show ticket's user name with that users company name.
Here, I have create relation for it that looks like below.
Ticket model :
public function requesters(){
return $this->belongsTo('App\User','requester_id');
}
Here requester_id is who create ticket.
Users model :
public function company()
{
return $this->belongsTo('App\Models\Admin\Company');
}
Company model :
public function Users()
{
return $this->hasOne('App\Users');
}
Here, I have written query to get users information that looks like below.
Ticket::with('requesters')->orderBy('subject','asc')->paginate(10);
Now, I want to fetch company information or request_id So what changes should I have to do in this query to fetch company info along with ticket and users ?
If you want to to Eager load multiple relationship you can put them all in single with like this:
Ticket::with('requesters','requesters.company')->orderBy('subject','asc')->paginate(10);
but if you load nested relationship you can use shorter notation - you can omit parent relationship, so it's enough to use here:
Ticket::with('requesters.company')->orderBy('subject','asc')->paginate(10);
Try this, it should work for this case:
Ticket::with('requesters')
->with('requesters.company')
->orderBy('subject','asc')->paginate(10);
I have 2 controllers & Models:
User Controller: (Model Relationship: $this->hasMany(Hero::Class);)
Hero Controller: Each hero has his own attributes, such as name, strength and life.
Model Relationship: ($this->belongsTo(User::class);)
Each user can own multiple heroes.
that means that USER ID: 1 may have 3 heroes: HERO ID 5, 20, 26..
My question: How to define the relationships like that and make laravel knows how to handle my user_heroes table?
The relationship i'm talking about is described in the following image:
How to I setup such kind of relationship in my laravel API?
If a User can have many heroes, and a Hero can also belong to many users, it is a many to many relationship. In Laravel the inverse of a many to many relationship is also a many to many relationship, and they are both described by belongsToMany().
https://laravel.com/docs/5.2/eloquent-relationships#many-to-many
So in your User model:
public function heros() {
return $this->belongsToMany(Hero::class);
}
And in your Hero model:
public function users() {
return $this->belongsToMany(User::class);
}
Laravel will assume the joining table is named hero_user, the 2 model names, singular, joined in alphabetical order. If you want to use user_heroes as you have in your image, you need to specify it:
return $this->belongsToMany(Hero::class, 'user_heroes');
(in both model methods).
I'm using Yii2 framework for the first time, trying to implement it in a project.
I've got a dropdown list of customers where i'd like to also show customer's company next to customer's name.
Customer and Company tables are related. Here's what it looks like in a Customer model:
public function getCompany()
{
return $this->hasOne(Company::className(), ['id' => 'company_id']);
}
So now i'm forming a dropdown list containing customer's name, email, phone and company name. Name, email, and phone belong to one table, so there's no problem with pulling them together. Here's what it looks like in Customer model:
public function getfullInfo()
{
return $this->name.' '.$this->phone.' '.$this->email;
}
I don't really understand this framework's logic. How do I pull in Company's name in above code?
Thank you guys.
Here's the correct code:
public function getfullInfo()
{
return $this->name.' '.$this->phone.' '.$this->email.' '.$this->company['name'];
}
This was so easy and I've wasted so much time on this.