nacha
4 weeks ago
221
5
Laravel

ErrorException (E_ERROR) Undefined variable: attributeValues (View: C:\wamp\www\site\resources\views\site\pages\products.blade.php) and BadMethodCallException

Posted 4 weeks ago by nacha

when I make a name of product in search bar and click enter there's this error

ErrorException (E_ERROR)
Undefined variable: attributeValues (View: C:\wamp\www\site\resources\views\site\pages\products.blade.php)
Previous exceptions
Undefined variable: attributeValues (0)

and when I check a value to filter product with size forexemple size :s with checkbox it return this error:

BadMethodCallException
Call to undefined method App\Models\Product::attribute_values()

relations:

class Product extends Model
{
 public function attributes()
    {
        return $this->hasMany(ProductAttribute::class);
    }
class ProductAttribute extends Model
{
    /**
     * @var string
     */
    protected $table = 'product_attributes';

    /**
     * @var array
     */
    protected $fillable = ['attribute_id', 'product_id', 'value', 'quantity', 'price'];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function product()
    {
        return $this->belongsTo(Product::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function attribute()
    {
        return $this->belongsTo(Attribute::class);
    }
class Attribute extends Model
{
  
 public function values()
    {
        return $this->hasMany(AttributeValue::class);
    }
class AttributeValue extends Model
{
 public function productAttributes()
    {
        return $this->belongsToMany(ProductAttribute::class);
    }

products.blade.php:

h6 class="title">By size </h6>
                            </a>
                        </header>
                       
                        
                        <div class="filter-content collapse show" id="collapse46">
                            <div class="card-body">
                           <form method="get" action="{{ route('products.index') }}"> 
                              @foreach($attributeValues as $attributeValue)
                              @if ($attributeValue->attribute_id == 1)
                               
                                    <label for="size" class="form-check">
                                         <input name="filter[attributeValue]" type="checkbox" value="{{ $attributeValue->value }}"
                                        @if (in_array($attributeValue->value, explode(',', request()->input('filter.attributeValue'))))
                                        checked
                                        @endif
                                         >
                                        <span class="form-check-label">
                                      {{ $attributeValue->value }}
                                      
                                        </span>
                                    </label>
                                    <!-- form-check.// -->
                            
                                </form>
                              @endif 
                             
                            @endforeach
                        
                            </div>

<button type="submit">apply filter</button>
<h6 class="title">By color </h6>
                            </a>
                        </header>
                       
                        
                        <div class="filter-content collapse show" id="collapse46">
                            <div class="card-body">
                            <form method="get" action="{{ route('products.index') }}"> 
                              @foreach($attributeValues as $attributeValue)
                              @if ($attributeValue->attribute_id == 2)
                               
                                    <label for="color" class="form-check">
                                         <input name="filter[attributeValue]" type="checkbox" value="{{ $attributeValue->value }}"
                                        @if (in_array($attributeValue->value, explode(',', request()->input('filter.attributeValue'))))
                                        checked
                                        @endif
                                         >
                                        <span class="form-check-label">
                                      {{ $attributeValue->value }}
                                      
                                        </span>
                                    </label>
                                    <!-- form-check.// -->
                            
                                </form>
                              @endif 
                             
                            @endforeach
<button type="submit">apply filter</button>

productController:

 public function index( Request $request)
    {   
        
       $attributeValues=AttributeValue::all();
      
        
        $products = QueryBuilder::for(Product::class)
          
            ->allowedFilters([
                AllowedFilter::exact('brand', 'brand_id'),
                AllowedFilter::custom('category', new FiltersCategories),
                AllowedFilter::custom('attributeValue', new FiltersAttributeValues),
                ])
            ->get();
        return view('site.pages.products', compact('products','attributeValues'));

FiltersAttributeValues.php:

<?php

namespace App\Filters;

use Spatie\QueryBuilder\Filters\Filter;
use Illuminate\Database\Eloquent\Builder;

class FiltersAttributeValues implements Filter
{
    public function __invoke(Builder $query, $value, string $property) : Builder
    {
        return $query->whereHas('attribute_values', function ($query) use ($value) {
            if (is_array($value)) {
                return $query->whereIn('value', $value);
            }

            return $query->where('value', $value);
        });
    }
}

please how to fix that thank you

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