Eloquont related data - laravel

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);

Related

How to order by the number of relationships

I am trying to create a "tag cloud" that lists the tag with the most relationships on a many-to-many pivot table schema.
I'm afraid I don't even know what to search in order to begin. I would appreciate any links or code examples, but prefer references so I can learn this once and for all.
I have a table named companies and a table named categories. There is a pivot table named category_company and the proper relationships are setup and working great.
Simply using withCount() should get you there.
$companies = Company::withCount('categories');
Now you are able to access the count like so.
foreach ($companies as $company) {
$company->categories_count; // gives out the count for this companies categories.
}
See the docs.

Inverse of belongsToMany

I got two Models:
Order
Invoice
Each Order can have many Invoices - and an Invoice can belong to many Orders.
So I can search for an Order and check: "Hey, which Invoices have been created for this Order?"
The other way round each Invoice can belong to multiple Orders, because maybe a customer ordered two products on the same day and so it would be great he'd only get one Invoice, which includes both orders.
So this is how I did this:
Invoice
public function orders()
{
return $this->belongsToMany(Order::class);
}
Order
public function invoices()
{
return $this->belongsToMany(Invoice::class, 'invoice_order');
}
This does work - but it does not seem right to change the table to the intermediate table invoice_order here. Do you have any thoughts on this? :-)
Thanks in advance for your thoughts :-)
Seperating the relation into a seperate pivot table is the commonly used method in laravel (and in most other frameworks) for many to many relationships.
It's easy to maintain, easy to get related models using many to many relationship, and if someone else needs to work on it in the future, they'll probably have used it in the past as well so wouldn't end up burning their heads.
The other method you could use is to create a json column on one of the tables (you can create on both tables as well if you want, but that's just extra overhead). Then you can store the ids of the related models in this json column. You can then join the tables using the json related commands provided by your database. Eloquent does not support relationships on json, but you can use this package staudenmeir/eloquent-json-relations to build relationships on json fields.
So overall, I'd suggest keeping a pivot table like the standard way, but if that just won't do, then you can try the json column method

Laravel many to many relation attach to multiple models

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.

Laravel Eloquent triple Relationship

I am stuck in situation where I need relation between 3 different tables. My tables are companies, products and Roles. Companies can assign multiple Roles to multiple products. The problem is Companies do not have any relationship with products. Products are added through admin.
Currently I have made a table company_product_role with structure company_id, product_id and role_id, the problem is how to make eloquent relation for insertion and retrieval. Either I am doing it correct or there is simple solution for it?
Any help will be appreciated.

Multi categories posts

I'm using laravel 5.6 and i have two tables categories and posts
I have created category_id in posts table which looks like
{id, category_id, title, description, created_at, updated_at}
I created a drop-down on the post create and edit form to select the category which works fine.
Now I am looking for something more advanced where a post can have multiple categories. I have changed belongTo to HasMany categories in post model.
I feel I am doing it the wrong way. Do i need to create another table i.e.,
post_categories
{id, category_id, post_id}
The reason i want to do this is because i have multiple posts which belong to multiple categories and my route is like this
site.com/categoryname/post-slug
So few posts appear in multiple categories.
You probably will need to use the pivot table. That way you can data mine. Even if you don't have a multi-select, you'll easily be able to collect posts linked to categories and vice versa. laraveldaily-good example. They use the sync method, one of my favs. when you save the form data you can just do something like App\Post::find($id)->categories()->sync(request()->input(categories')) and laravel will handle the rest for you.
Your relationships look like they are thought out. to me, it looks like your on the right track.
Just use the belongsToMany relationship instead of HasMany
Laravel has great documentation on this: laravel many to many

Resources