Products related to category - php

I'm facing an issue to order products by price related to category. When I click "sort by price" it shows all products from all categories ordered by price. I need to present only the products that relate to the category that is in the url, ordering by price.
This is the controller:
class SortController extends MainController{
public function sortBy($category_url, Request $request)
{
$sort = $request->get('sort', 'asc');
$products = Product::orderBy('price', $sort, $category_url)->get();
return view('content.sort')->with('products', $products);
}
}
This is the view:
extends('master')
#section ('content')
<form id="order-product-form" method="get" action=">>
{{url('shop/{category_url}/sort=ASC')}}"enctype="multipart/form-data">
#if ($products)
#foreach($products as $product)
{{ $product['title']}}

To filter a model based on a related model you use whereHas()
$products = Product::whereHas('category', function ($query) {
return $query->where('id', '=', $category_id); // You filter here by the required category
});
$products->orderBy('price', $sort, $category_url)->get();

You need $category->products->title.
Do you have a relation between models Category and products?

Related

How to show categories that it's products relation have a specific attribute?

The Category model hasMany Products that belongsTo a Category, the products table have category_id and type columns.
I need to show just the categories whose products' type have a specific value.
I tried this but didn't get the correct results:
return Category::with('products')->get()->filter(function ($query) {
return $query->products
->where('type_id', 1);
// or return $query->products->contain('type_id', 1);
});
You can use whereHas() method :
$typeId = 1;
Category::with('products')->whereHas('products', function ($query) use ($typeId) {
return $query->where('type_id', $typeId);
})->get();
I found the working Answer:
return Category::with(['products' => function ($q) {
return $q->where('type', "small");
}])->whereHas('products')->get();

How to get category wise all product without one specific product ID in laravel?

I have a category table, product table.I want to show category wise all products except one specific product id which comes from request
Here is the model relationship
Product Model
public function category(){
return $this>belongsTo(Category::class);
}
CategoryModel
public function products(){
return $this->hasMany(Product::class);
}
I'm getting all products by this way
$product = Product::findOrFail(10);
$category_products = $product ->category->products;
But I want to get all products except product_id 10. How can I get that?
To show category wise products and exclude specific products from related collection you can query category of product and eager load related products using with but remove your desired product from collection using a closure method
$product = 10;
$category = Category::with(['products'=> function($query) use($product) {
$query->where('id','!=', $prodcut);
}])
->whereHas('products', function (Builder $query) use($product) {
$query->where('id', $product);
})->get();
You can constrain eager relations with the with() method, passing a closure.
$id = 10;
$product = Product::findOrFail($id)
->with(['category.products' => fn ($query) => $query->where('id', '!=', $id)])
->get();
$otherProducts = $product->category->products;
pre php 7.4
$product = Product::findOrFail($id)
->with(['category.products' => function ($query) use ($id) {
return $query->where('id', '!=', $id);
}])
->get();
on second thoughts, a more efficient way might be to just query the products table directly:
$otherProducts = Product::query()
->where('category_id', $categoryId)
->where('id', '!=', $productId)
->get();

How to use eloquent to retrieve a column from another table

I have two tables
product: id|category ...
category: id|name ...
product.category is a foreign key linked to category.id . I am building a basic CRUD and I would like to display all Products in the the product table as well as the name of the category they belong to rather than their category ID. TO do this, while searching the laravel documentation I came across the query builder and I achieved my goal.
public function index()
{
$products = \DB::table('products')
->join('categories', 'products.category', '=', 'categories.id')
->select('*')
->get();
return view('product' ,compact('products'));
}
Under my models for product and category I have created the appropriate relationships.
product.php :
public function category()
{
return $this->belongsTo('App\Category');
}
category.php :
public function products()
{
return $this->hasMany('App\Product');
}
I keep hearing about the power of Eloquent and was wondering how I could achieve a similar result with eloquent and if eloquent is designed for such operations or if the query builder is the right way to go.
Every tutorial online seems to only use the post and comments scenario of getting all comments belonging to a post.
You can use this code
public function index()
{
$products = Product::with('category')->get();
return view('product' ,compact('products'));
}
In blade
#foreach($products as $product)
{{$product->name}}
{{$product->category->name ?? ''}}
//or
#if ($product->category)
$product->category->name
#endif
#endforeach
Also if in project table foreign key is not equal category_id. In your case
public function category()
{
return $this->belongsTo('App\Category', 'category');
}

Laravel-5 whereHas Clause dosen't work as expected

I have categories and products tables. They are related with Many to Many relationships.. I want to get products by categories and selected price range but whereHas clause dosen't work
$products = Product::whereHas('categories',function ($query) use ($slug){
$query->where('category_slug',$slug);
})->whereBetween('price',[100,200])->get();
Category Model;
public function products(){
return $this->belongsToMany('App\Product','category_product','category_id','product_id');
}
Product Model;
public function categories() {
return $this->belongsToMany('App\Category','category_product','product_id','category_id');
}
So, what is my mistake in here ?

Hiding empty categories in Laravel

How can I hide empty categories when I query to display them? Empty categories are those that no products are assigned to them..
Here is my controller
public function showSubCats($categoryId) {
$subcats = SubCategories::where('category_id', '=', $categoryId)->get();
return View::make('site.subcategory', [
'subcats' => $subcats
]);
}
Here is the view
#if(count($subcats) > 0)
<div class="row">
#foreach($subcats as $i => $subcategory)
// display categories
#endforeach
#else
There is no products assigned to this category
</div>
#endif
This is my SubCategories model
public function products()
{
return $this->hasMany('Product', 'category_id');
}
public function subcategories()
{
return $this->hasMany('SubCategories', 'category_id');
}
public function lowestProduct() {
return $this->products()->selectRaw('*, max(price) as aggregate')
->groupBy('products.product_id')->orderBy('aggregate');
}
In product table I have column which is sub_cat_id and holds category in which is assigned. If is 0 is not assigned to any category.
How can I hide empty categories now?
You should use where in addition to your model
return $this->hasMany('Action')->where('sub_cat_id', 1);
Note :
I believe that you neeed to take the records only that has sub_cat_id as 1. If not change it to 0 or accordingly.
Hope this helps you

Categories