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
Related
I have this structure:
model: OrderItem
belongsTo ProductInfo
model: ProductInfo
hasMany OrderItem
hasMany Barcode
model: Barcode
belongsTo ProductInfo
How can I make an easy accessible relationship in the OrderItem to get the Barcodes that match the same product_info_id? The product_infos table is of course not a classic intermediate table containing the foreign keys.
I know this is probably a very simple question since the relationship is also very basic, but I'm kind of confused with all the answers I find online and in the Laravel docs for slightly different situations. F.e. hasManyThrough is not working out in any way here.
This relationship is correct you can access barcode like
$order->ProductInfo->barcodes
This will return an array of barcodes
Instead of using the pivot model, try this.
OrderItem belongs to many Barcode
Then the Barcode model,
table: barcodes
Barcode belongs to many OrderItem
And last create a pivot table
php artisan make:migration create_barcodes_order_items_table and make two columns for foreign_ids, order_item_id and barcode_id.
It would automatically take care of your relationship unless you didn't do anything outside the pattern.
Then you can get all barcodes of order and all orders for a barcode.
$order->barcodes
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();
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?
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 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.