Laravel belongsToMany Relationship - php

Having a belongsToMany relation on the same model (Team Model) through custom pivot table (related_teams)
and my relation is like the following
now attaching and detaching is working just fine , however as you see every team belongs to a city
.
so my question is how can i get a list of distinct cities from those attached teams

It depends what you want to achive but for example
$this->attachedHotels->groupBy('city_id');
https://laravel.com/docs/6.x/collections#method-groupby
or
$this->attachedHotels->pluck('city_id')->unique();
https://laravel.com/docs/6.x/collections#method-pluck
https://laravel.com/docs/6.x/collections#method-unique

Related

establish a relationship among a table with many different table

i have a table (piovt table) that links the invoice id and the service id, but the service id does not come from one table but comes from different tables. here is an example of that.
i tried use polymorphic relations but i only get the last recored not all recoreds related to the invoice
so how to get the services from invoice table?
You can check the Has One Through relationship.
Laravel documentation states "The "has-one-through" relationship defines a one-to-one relationship with another model. However, this relationship indicates that the declaring model can be matched with one instance of another model by proceeding through a third model."
You can read more and get code examples here
Laravel has one through relationship

name this laravel5.4 eloquent relationship in three tables

I studied eloquent relationships in laravel, i love to use them but today am confuse about relation the following three tables has.
I have a relation between three tables like:
1) companies {id, company_name}
2) screens {id, screen_name}
3) company_screen {id, company_id, screen_id, connected[yes/no] }
what type of relation is this in laravel?
how can i fetch screens which are connected(yes), for a company?
after posting my question i read many to many relation in https://laravel.com/docs/5.4/eloquent-relationships#updating-many-to-many-relationships
it's just many to many relation with and extra column in pivot table.
thanks

hasMany vs belongsToMany in laravel 5.x

I'm curious why the Eloquent relationship for hasMany has a different signature than for belongsToMany. Specifically the custom join table name-- for a system where a given Comment belongs to many Roles, and a given Role would have many Comments, I want to store the relationship in a table called my_custom_join_table and have the keys set up as comment_key and role_key.
return $this->belongsToMany('App\Role', 'my_custom_join_table', 'comment_key', 'role_key'); // works
But on the inverse, I can't define that custom table (at least the docs don't mention it):
return $this->hasMany('App\Comment', 'comment_key', 'role_key');
If I have a Role object that hasMany Comments, but I use a non-standard table name to store that relationship, why can I use this non-standard table going one way but not the other?
hasMany is used in a One To Many relationship while belongsToMany refers to a Many To Many relationship. They are both distinct relationship types and each require a different database structure - thus they take different parameters.
The key difference is that in a One To Many relationship, you only need the two database tables that correspond to the related models. This is because the reference to the relation is stored on the owned model's table itself. For instance, you might have a Country model and a City model. A Country has many cities. However, each City only exists in one country. Therefore, you would store that country on the City model itself (as country_id or something like that).
However, a Many To Many relationship requires a third database table, called a pivot table. The pivot table stores references to both the models and you can declare it as a second parameter in the relationship declaration. For example, imagine you have your City model and you also have a Car model. You want a relationship to show the types of cars people drive in each city. Well, in one city people will drive many different types of car. However, if you look at one car type you will also know that it can be driven in many different cities. Therefore it would be impossible to store a city_id or a car_id on either model because each would have more than one. Therefore, you put those references in the pivot table.
As a rule of thumb, if you use a belongsToMany relationship, it can only be paired with another belongsToMany relationship and means that you have a third pivot table. If you use a hasMany relationship, it can only be paired with a belongsTo relationship and no extra database tables are required.
In your example, you just need to make the inverse relation into a belongsToMany and add your custom table again, along with the foreign and local keys (reversing the order from the other model).
Try to understand with text and a figure.
One to One(hasOne) relationship:
A user has(can have) one profile. So, a profile belongs to one user.
One to many(hasMany):
A user has many(can have many) articles. So, many articles belong to one user.
Many to many(BelongsToMany):
A User can belong to many forums. So, a forum belongs to many users.

How to set up database relation so that a category may belong to multiple parent category

I am currently using Laravel (PHP framework) in order to construct an ecommerce site.
The site will have a lot of categories(for products) of which there is ruffly 100-150 and will probably be more as there will be a backend site to add more.
Some times it will be necessary for a category to appear in more than 1 parent category on the site.
Category Relationships I am trying to achieve:
A category can have many child categories.
A category may have more than one parent category.
I am very confused as to how to set up the second of these two relationships correctly within Laravel.
So my question is:
How do I set up a database structure and Model relations so that a category can belong to many other category without any duplication in the categories table.
I would like to know what tables/columns I need and also what types of relationships need to be set in the models please.
This model seems to work:
I have a table called category_category and relation:
public function parentCategories()
{
return $this->belongsToMany('TottonTimber\Category', 'category_category', 'category_id', 'parent_id');
}
public function childCategories()
{
return $this->belongsToMany('TottonTimber\Category', 'category_category', 'parent_id', 'category_id');
}
Yet this doesn't seem like the correct way of doing it as both are "belongsTo"
In Many to Many to relations. Both models do have a belongs to relation with the other. For example in classic User & Roles Scenerio, User Belongs to Many Roles & Roles Belongs to Many Users.. So as you see 'Belongs to' relationship both sides. Here as you have same model for both ends of your relations, you have to put 'belongsto' for both your relation definations. That is perfectly ok it seems.

Please explain hasManyThrough relation

Is there any way to get all relational data with hasmanyThrough of third model that has many records with the relation with second model?
As example Main model : Exam id batch_id
relation- belongTo-Batch
second model : Batch id batch_name relation-hasMany-Exam and hasMany-Student
third mode :Student id name batch_id relation- belongTo-Batch
So using Exam model when I try to get batch that belong to it then I can retrieve it but How can I get all student through Exam model related to that batch? I have tried with hasManyThrough relation but not working...
If you were to use this in Exam:
public function students() {
return $this->hasManyThrough('Student', 'Batch');
}
This won't work, as hasManyThrough assumes that both Student and Batch belong to Exam (Exam belongs to batch)
The easiest way would be something like this: Exam::find(1)->batch->students

Categories