Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

With

$products = Brand::with('product')->where('id', '=', '1');

You can ready use category

foreach($products as $product){
	echo $product->category->id;
}
0

Actually I may have jumped the gun in marking this as solved, hopefully I don't need to open a new thread.

Category::with('product')->where('id', '=', $id)->get();

produces this query:

	select * from `products` where `products`.`category_id` in ('1')

and

Brand::with('product')->where('id', '=', $id)->get();

produces:

	select * from `products` where `products`.`brand_id` in ('1')

I want to have access to all Brands where the category_id is 1 so I am using the first (Category::with('product')->where('id', '=', $id)->get();)

which returns the following array:

..so I have no access to the Brand description through this. Any ideas?

Thanks!

Last updated 2 years ago.
0

Do you want to get all brands of products in a category????

Frontend like:
Brand1 | Brand 2 | Brand 3
List Products
Product 1 | Product 2 | Product 3 | Product 4
Last updated 9 years ago.
0

Yes so basically selecting a specific Category (for instance Paper, Chair, Tables) will give me all brands under each specific category so selecting:

Paper will list Brand1 and Brand6 Chair will list Brand2 and Brand4

and so on depending on the type of product. Hope this makes sense

0

@paulocr, I think you should change your Product model to use belongsTo relationship, since category_id and brand_id are in the products table. I don't know if it will work this way.

About your question, I think hasManyThrough is the way to go. Maybe this will work:

class Category extends Eloquent {

    public function brands()
    {
        return $this->hasManyThrough('Brand', 'Product');
    }

}
0

thank you, I will test it out

0

Thanks for all your help. Your help + eager loading + a pivot table + a lot of frustration seem to have given me what I needed.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

paulocr paulocr Joined 6 Oct 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.