dimanche 30 octobre 2022

Dynamic Scopes not working properly when dealing with tags->products() (many to many relationship) Laravel Livewire

its throwing me this error:

Call to a member function products() on null

i want the filter to be changed in real time and it works too just not working properly with many to many relationship:

this doesnt work

$tag1 =  request()->query('tag');  
$tag = Tag::where('name', $tag1)->first();
$products = $tag->products()->SortFilterBy($this->sortBy)->InStock()->paginate($this->perPage);

but when i manually type this

$products = $tag->products()->SortFilterBy('featured')->InStock()->paginate($this->perPage); //
Works But not in real time
$products = Product::SortFilterBy($this->sortBy)->InStock()->paginate($this->perPage); //WORKS IN REAL TIME

i am using livewire for this.

The Scope:


public function scopeSortFilterBy($query,$sortBy = "")
{
switch ($sortBy) {
case 'featured':

                return $query->where('featured','=', -1);
    
                break;
    
            case 'low':
           
                return $query->orderBy('sale_price', 'asc');
               
                break;
            case 'heigh':
                return $query->orderBy('sale_price', 'desc');
                break;
            default:
    
            return $query;
            
            break;
        }
      
    }

it do shows me the value of dd($this->sortBy) whenever i change the dropdown.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire