products with categories using category_products - laravel

I have a Relation of products with categories using category_products as a pivot table. I want to get/take 5 products from each category but it not working correctly.
I have tried all the methods known to me I could find the solution
$query = $this->getQuery();
$query->where('parent_id','0')->with('categoryProducts',function($q1){
$q1->with('product',function ($q){
$q->orderBy('sold', 'desc');
})->take(4);
})->whereHas('categoryProducts');
return $query->get();

Related

Laravel eloquent many to many relation deep conditions

I have two Model named Product and Category. Admin can change the status of Product and Category.
Here Product and Category has many to many relations. So that, all Product can belongs to multiple Category
Now at my user panel,
I want to show that all Product whose all Category's status is active.
you can do this in your product model
public function categories(){
return $this->belongsToMany(Category::class, CategoryProduct::class, 'product_id', 'category_id');
//->withPivot([]); if you need anything from the pivot table for any reason you can add the column names in the array
}
this is assuming that the pivot table have the columns product_id and category_id
$products = Product::with('categories')
->whereHas('categories', function ($query) {
$query->where('categories.status', 'active');
})
->get();
inside the where the word categories is the table name
The solution is:
$products = Product::with('category')
->whereHas('category', function ($query) {
$query->where('status', 'active'); // use your column & condition
})
->get();
It works for me, got all the products that have all the categories active. First time I thought complicated so I didn't solve.
$products = Product::whereDoesntHave('catagories',function($category){
$category->where('status','!=',1);
});

How do I query data from a pivot table in Laravel 7 [duplicate]

Searching from pivot table using laravel.
Here is my table structure:
Product
id
name
Categories
id
name
product_category (Pivot table)
id
category_id
product_id
//products can have multiple categories
Product model:
public function categories(){
return $this->belongsToMany(Category::class, 'product_category');
}
What is the best way to search all products by category id?
Currently I am doing this way, and it seems not an efficient way:
//Controller
$categories = product_category::where('category_id',1)->get();
Now I have to loop through categories and then get Products and pass it to views? Any idea how to do this in an efficient way?
For this you could use the whereHas() method:
$categoryId = 1;
$products = Product::whereHas('categories', function ($query) use($categoryId) {
$query->where('id', $categoryId);
})->get();
The above will return all products that are in the Category where the id is equal to $categoryId.
You can eager load products for a given category. Try:
$category = Category::with('products')->where('category_id',1)->find(1);
When you do this, only 2 database queries will be executed: one for loading the category, and one for loading related products.
Then in your Blade view you can do:
#foreach($category->products as $product
{{ $product->name }}
#endforeach
You can use this inside your method in controller..this only works when $request->$query(search) have value .then here we use wereHas for get the relationship of model and with->() using for get pivot table values
->when($request->query('search'), function ($query)use($request) {
$q= $request->query('search');
return $query->whereHas('relation name', function (Builder $query) use ($q) {
$query->with('pivot table name.column name')
->where('pivot table name.column name', 'like', "%{$q}%")
});})

best approach to search from pivot table using laravel

Searching from pivot table using laravel.
Here is my table structure:
Product
id
name
Categories
id
name
product_category (Pivot table)
id
category_id
product_id
//products can have multiple categories
Product model:
public function categories(){
return $this->belongsToMany(Category::class, 'product_category');
}
What is the best way to search all products by category id?
Currently I am doing this way, and it seems not an efficient way:
//Controller
$categories = product_category::where('category_id',1)->get();
Now I have to loop through categories and then get Products and pass it to views? Any idea how to do this in an efficient way?
For this you could use the whereHas() method:
$categoryId = 1;
$products = Product::whereHas('categories', function ($query) use($categoryId) {
$query->where('id', $categoryId);
})->get();
The above will return all products that are in the Category where the id is equal to $categoryId.
You can eager load products for a given category. Try:
$category = Category::with('products')->where('category_id',1)->find(1);
When you do this, only 2 database queries will be executed: one for loading the category, and one for loading related products.
Then in your Blade view you can do:
#foreach($category->products as $product
{{ $product->name }}
#endforeach
You can use this inside your method in controller..this only works when $request->$query(search) have value .then here we use wereHas for get the relationship of model and with->() using for get pivot table values
->when($request->query('search'), function ($query)use($request) {
$q= $request->query('search');
return $query->whereHas('relation name', function (Builder $query) use ($q) {
$query->with('pivot table name.column name')
->where('pivot table name.column name', 'like', "%{$q}%")
});})

Eloquent ORM; M-N relationship limit results

I have a Many to Many relationship (Category belongsMany Product and Product belongsMany Category).
I want to select all categories and each category I want to get 10 products by using Eloquent.
My code:
$categories = Category::with(['products' => function($query)
{
$query->limit(10);
}])->get();
It just get 10 products for all categories instead of 10 products for each category.

Laravel paginate items in multiple categories

I have 2 tables, products and categories. Products belong to different categories, and categories are formed in tree structure that allows for parent category and child category relationship. So given a specific category, how should I write query to get paginated products that belong to this category and all its child categories?
I forgot to mention that in my settings, products and categories are many to many relationship, there is a pivoting table named product_catrgory.
$ids = $category->children()->lists('id');
$ids[] = $category->id;
$products = Product::whereIn('category_id', $ids)->paginate(20);
Maybe there is other better method but you could do it this way:
$ids = [];
$ids[] = $category->id;
foreach ($category->children as $child) {
$ids[] = $child->id;
}
$products = Product::whereIn('cat_id', $ids)->paginate(15);
In my opinion the simplest way to do this is to first get category id and its all child category id recursively. And query the database using whereIn(array of category id) to get all the products. I do not think you can do it in one single query.

Resources