Laravel many to many relation attach to multiple models - laravel

I have many to many relation with pivot table. I know that I can use detach, attach and sync on a model , but is there a way to do this for multiple models with one call ?
For example I have Category and Product models and I want to assign same categories to multiple products.

Related

When polymorphic relation shouldn't be used?

Is it okay to use a polymorphic relation when there are lets say 6 common columns and 2 columns with different names?
I need to track car maintenance and refueling.
maintenances - table
-date
-km_driven
-info (refers to maintenance info )
refuelings - table
-date
-km_driven
-amount (refers to amount in liters)
So, should i use polymorphic relationship or not? Is it ok if there are more different columns per model?
IMHO for your case I will go for single table inheritance (STI), but you need a library like this tightenco/parental or this one Nanigans/single-table-inheritance.
Laravel codebase has no support for STI without an external library. You can try to use polymorphic relation to solve your case but you will end up with 3 different tables. I think you want to use a single table with two or even three models so my advice is to try one of the STI library above.
STI is appropriate when your models have shared data/state. Shared behavior is optional because can be defined per Model. An example could be different type of vehicle Models: Car, Truck, Bike etc..
With Polymorphic Relations instead, a model can belong_to several models with a single association. This is useful when several models do not have a relationship or share data with each other, but have a relationship with the polymorphic class. An example could be the Comment Model that can belongs to other Models: User, Post, Image etc..

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?

Eloquont related data

I have a ProductCategory say "Dresses". Products are related to product category and images are related to Products. I want the list of records with both products, product images for a particular ProductCategory id using Laravel 5.2.
I tried:
$productCategories = ProductCategory::find(1)->products;
This give me related products, but now I want all the related images to the products in the result.
look for eloquent nested relationships eager loading. need to know what kind of relationships and relationship names you are using.
haven't tested but it should work something like this. if many to many relationship come by you may need to use a foreach to loop through finding related model of each.
$productCategories = ProductCategory::with('products', 'products.images')->findOrFail(1);

Magento save many to many models

I have created a custom module with 3 models (student, room, studentroom).
The studentroom is used to store the relation between student & room (manytomany).
I have maked the form + grid to the students and rooms and i can create both of them. i want now to add a multiselect in the student form to select the rooms (until now its ok). When i save or edit my student how can i save or display also the association in studentroom.
Anyone has an idea ? A module with the same functionalities ?
Thx for advance
Magento's ORM has no built-in methods for the sort of one to many and many to many relationships you're describing above. As such, it's up to each individual developer to implement their own save methods that (before or after calling parent::save()) handle any extra relationships an object might have.

laravel 4 polymorphic relationships

I'm trying to get my head around using polymorphic relationships in Laravel 4, but I can't work out how to do the following:
I have a users table, customers, suppliers and partners tables and a userhistories table which has a one to many with users and morph many (called historable) with customers, suppliers and partners.
I want to get the latest 10 userhistories for the (Auth::user) logged in user that are linked to the customers table without including those for the other two tables, but I can't work out the Fluent query(s) to let me do this. Help! Thanks.

Resources