Laravel many to many relations query collection with - laravel

I have models:
User belongsToMany group,
User belongsTo class,
User belongsTo grade
Group belongsToMany user
when i do $group->user->with('class') i received an error that collection::with does not exist.
i am trying to achieve a collection of users that belong to the group with the one to many relation information. Any suggestions how this should be done?

Use a relationship query:
$users = $group->users()->with('class')->get();

Related

hasOne or BelongsTo in Laravel?

I have two tables: Photos and ProductPhotos where ProductPhotos is:
ProductPhotos
_______________
product_id | photo_id
And Photos is:
Photos
_______________
id | photo
Does it mean that relation beetween ProductPhotos -> Photos as belongsTo or hasOne yet?
Cause photo_id is foreign key, I guess it is belogsTo
A belongsTo is used to define inverse of the relationship for both hasOne (One to One) and has Many (One to Many) relationship. It would not be possible to tell the relationship with foreign key. You need to define relationship in your relevant model.
In your case, relations can be
ProductPhotos belongsTo Products
Products can have hasOne or hasMany relationship with productPhotos. Depends on the relationship defined on the Product Model
You can have detail understanding on relationship on laravel Docs Relationships
I would set up the relationships with
photos->hasOne->ProductPhotos->belongsTo->Products
Products->hasMany->ProductPhotos->belongsTo->Photos
This will allow for the use of Associate & Disassociate as well as the hasManyThrough relationship
https://laravel.com/docs/5.5/eloquent-relationships#has-many-through
This will also depend on your use of the images of the products
Hope this helps
In this scenario it implies that you have a Products table, so your relationship seems to be from many to many.
Use in both tables:
belongsToMany
https://laravel.com/docs/5.7/eloquent-relationships#many-to-many

How to create two sided many to many polymorphic relationships in Laravel?

I have multiple models which can and can be followed by each other i.e. Business, Agency, Vendor, User.
The schema of the above scenario is as follows:
id follower_id follower_type followable_id followable_type
Is there any relationship for the above case in Laravel? How can I create relationship and use eloquent methods?

Can Laravel hasOne be used with belongsToMany?

Let's say I have two tables:
Users: id, name, country_id
Countries: id, name
Of course each user can only have one country, but each country is assigned to multiple users.
So would it be safe to have a User model that utilizes hasOne and a Country model that uses belongsToMany method?
Documentation makes it seem like you can't mix and match different types of relationships.
What you are describing is actually a One To Many relationship, where one country has many users. Your Country model should utilize a hasMany relationship, while your user would have a belongsTo relationship.
#Andy has already answered well.
Anyway, my advice is to always think in the following way to create a One-To-One, One-To-Many, or a Many-To-Many relationship:
In the table with the foreign key (if any) use belongsTo
In the other table without the foreign key use hasOne or hasMany
In any of them have a foreign key, you have a Many To Many relationship and you must use belongsToMany in both of them (you need the pivot table, of course).

Laravel eloquent relationship between two many to many related tables

I have many to many related two tables, "roles" and "users" with pivot table as "user_roles".
In Role model
$this->belongsToMany('users', 'user_roles');
In User model
$this->belongsToMany('roles', 'user_roles');
Is this a valid relation?
You want a pivot table and a many to many relationship using a pivot table like you said.
What you are trying to do is explained in Laravel documentation - Eloquent
No. The first parameter should be the class name of the related model:
$this->belongsToMany('User', 'user_roles');
$this->belongsToMany('Role', 'user_roles');
Otherwise you should be fine if you foreign keys follow the convention...
See the documentation for more info

Eloquent hasManyThroughOne

Laravel 4.1 introduced the hasManyThrough relationship. This assumes 2 relating hasMany relationships. I however would like to retrieve the hasMany relationships of a belongsTo relationship instead.
Project (id, contact_id, ...)
Contact (id, ...)
Address (id, contact_id, ...)
For each project, I would like to get all addresses.
I managed to do this using a belongsTo() relationship and some additional table joining. However, a belongsTo relationship binds a single object, instead of an array.
So my thoughts are I either need to:
... be able to override the LIMIT 1 behavior on belongsTo relationships
... or be able to override the hasManyThrough to work with a belongsTo as intermediate relationship.
It sounds like you are trying to set up a many-to-many relationship between Projects and Contacts, with a one-to-one relationship between Address and Contact. If that is the case you will need to create a pivot table "project_contact" with columns "project_id" and "contact_id" as well as any other columns (timestamps(), etc). Then you can set a "belongsToMany('Project')" relationship on the Contacts.
I'm not sure if that is what you're after, but it solves the problem as I understand it.

Resources