Filtrar por categoría
Comprar curso

72. Filtrar por categoría


2 comentarios

Inicia sesión para comentar

Comentarios:

  • Alexander Nieves

    Alexander Nieves hace 1 mes

    no se si pueda compartirte mi codigo 

    filter.php

    <?php


     

    namespace App\Livewire;


     

    use App\Models\Option;

    use App\Models\Product;

    use Livewire\Attributes\On;

    use Livewire\Component;

    use Livewire\WithPagination;


     

    class Filter extends Component

    {

        use WithPagination;

        public $family_id;

        public $category_id;

        public $features;

        public $options;

        public $selected_features=[];

        public $orderBy=1;

        public $search;


     

        public function mount()

        {

            $this->options=Option::when($this->family_id, function ($query) {

                $query->whereHas('products.subcategory.category', function ($query) {

                    $query->where('family_id', $this->family_id);

                })->with([

                    'features' => function ($query) {

                        $query->whereHas('variants.product.subcategory.category', function ($query) {

                            $query->where('family_id', $this->family_id);

                        });

                    }

                ]);

            })->when($this->category_id, function ($query) {

                $query->whereHas('products.subcategory', function ($query) {

                    $query->where('category_id', $this->category_id);

                })->with([

                    'features' => function ($query) {

                        $query->whereHas('variants.product.subcategory.category', function ($query) {

                            $query->where('category_id', $this->category_id);

                        });

                    }

                ]);

            })

            ->get()->toArray();

        }




     

        #[On('search')]

        public function search($search)

        {

            $this->search=$search;

        }


     

        public function render()

        {

            $products = Product::when($this->family_id, function ($query) {

                $query->whereHas('subcategory.category',function($query){

                    $query->where('family_id', $this->family_id);

                });

            })

            ->when($this->category_id, function ($query) {

                $query->whereHas('subcategory',function($query){

                    $query->where('category_id', $this->category_id);

                });

            })

            ->when($this->orderBy == 1, function($query){

                $query->orderBy('created_at','desc');

            })

            ->when($this->orderBy == 2, function($query){

                $query->orderBy('price','desc');


     

            })

            ->when($this->orderBy == 3, function($query){

                $query->orderBy('price','asc');


     

            })

            ->when($this->selected_features, function($query){

                $query->whereHas('variants.features',function($query){

                    $query->whereIn('features.id',$this->selected_features);

                });

            })

            ->when($this->search, function($query){

                $query->where('name','like','%'.$this->search.'%');

            })

            ->paginate(12);


     

            return view('livewire.filter', compact('products'));

        }

    }


     

    • Victor Arana Flores hace 1 mes

      Hola Alexander, lamentablemente por este medio no puedo analizar tu código. Pero si me haces preguntas puntuales estaré encantado de ayudarte.

    • Alexander Nieves hace 1 mes

      gracias victor ya solucione muy amable solo que cuando cree la ruta en web estaba apuntando la ruta de category a family ya lo solucione graciass 

  • Alexander Nieves

    Alexander Nieves hace 1 mes

    amigo buen dia ya aplique exactamente esa solucion que brindas el codigo tal cual me sigue arrojando el Undefined array key "features" no se que pueda hacer 

    • Alexander Nieves hace 1 mes

      ayudame en lo posible porfavor

    • Victor Arana Flores hace 1 mes

      Hola Alexander, segun lo que dice el error es que estas intentando acceder al indice del array con ‘features’ y ese indice no existe. Revisa bien el nombre que le has dado y vuelve a intentarlo.