nacha
3 days ago
104
4
Laravel

Illuminate \ Database \ QueryException (42S22) SQLSTATE[42S22]: Column not found: 1054 Champ 'category_id' inconnu dans where clause (SQL: select * from `products` where `category_id` = 2)

Posted 3 days ago by nacha

when I check for exemple category:Men to filter products by category return this problem

SQLSTATE[42S22]: Column not found: 1054 Champ 'category_id' inconnu dans where clause (SQL: select * from `products` where `category_id` = 2)

and when I check brand it works but when I check two brands and click filter it return just the products of the last or first brand I check (not both) I check prada and gucci it reurn products of prada or gucci not both thank you

productcontroller

public function index(Request $request)
    {   
       $values=ProductAttribute::all();
        $categories = Category::all();

        $brands = Brand::all();
    
        $products = QueryBuilder::for(Product::class)
            ->with('categories')
            ->with('brand')
           
            ->allowedFilters([
                AllowedFilter::exact('brand', 'brand_id'),
                AllowedFilter::exact('category', 'category_id'),
                AllowedFilter::exact('value', 'value'),
                ])
            ->get();
    
        return view('site.pages.products', compact('products','brands','categories','values'));
       
}

products.blade.php

    <p>Brands</p>
                <form method="get" action="{{ route('products.index') }}"> 
@foreach ($brands as $brand)
    <label class="m-checkbox">
        <input
            name="filter[brand]" type="checkbox" value="{{ $brand->id }}"
            @if (in_array($brand->id, explode(',', request()->input('filter.brand'))))
                checked
            @endif
        >
        {{ $brand->name }}
    </label>
@endforeach
<button type="submit">apply filter</button>
<p>Categories</p>
<form method="get" action="{{ route('products.index') }}"> 
@foreach ($categories as $category)
@if ($category->id!=1)
    <label>
        <input
            name="filter[category]" type="checkbox" value="{{ $category->id }}"
            @if (in_array($category->id, explode(',', request()->input('filter.category'))))
                checked
            @endif
        >
        {{ $category->name }}
    </label>
    @endif
@endforeach

<button type="submit">apply filter</button>

Please sign in or create an account to participate in this conversation.