Sokol

Sokol

Member Since 6 Months Ago

Experience Points
4,830
Total
Experience

170 experience to go until the next level!

In case you were wondering, you earn Laracasts experience when you:

  • Complete a lesson — 100pts
  • Create a forum thread — 50pts
  • Reply to a thread — 10pts
  • Leave a reply that is liked — 50pts
  • Receive a "Best Reply" award — 500pts
Lessons Completed
46
Lessons
Completed
Best Reply Awards
0
Best Reply
Awards
  • start your engines Created with Sketch.

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • first-thousand Created with Sketch.

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • 1-year Created with Sketch.

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • 2-years Created with Sketch.

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • 3-years Created with Sketch.

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • 4-years Created with Sketch.

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • 5-years Created with Sketch.

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • school-in-session Created with Sketch.

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • welcome-newcomer Created with Sketch.

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • full-time-student Created with Sketch.

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • pay-it-forward Created with Sketch.

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • subscriber Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • evangelist Created with Sketch.

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • chatty-cathy Created with Sketch.

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • lara-veteran Created with Sketch.

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • 10k-strong Created with Sketch.

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • lara-master Created with Sketch.

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • laracasts-tutor Created with Sketch.

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • laracasts-sensei Created with Sketch.

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • top-50 Created with Sketch.

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

Level 1
4,830 XP
May
12
4 months ago
Activity icon

Awarded Best Reply on Laravel Fully Dynamic Catalog Filter

Problem solved. I think i shall leave my way to do it for somebody who wants to check out how its done.

Properties are in hasMany relation with products with property value pivot

Here is the code of function that retrieves them from database based on products builder. Works fast enough even for 5k products and 15 possible properties.

$properties = $products->flatMap(function ($item){
          return $item->properties->where('filter', 1);
        })->groupBy('id');
        $properties = $properties->map(function ($item){
            $item = $item->unique('pivot.property_value');
            return [
                'id' => $item->first()->id,
                'name' => $item->first()->name,
                'values' => $item->pluck('pivot.property_value')
            ];
        });

View code to display form. $filtersActive is simple collect(json_decode($request->filters)) at controller

@foreach($filters['properties'] as $filter)
        @if(count($filter['values']) > 1 || $filtersActive->keys()->contains($filter['id']))
            <div class="multifilter multifilter_material @if($filtersActive->keys()->contains($filter['id'])) multifilter_active @endIf">
                <div class="button-dropdown">
                <span class="button button_left button_s arrow-bottom multifilter__button">
                    <span class="multifilter__title">
                        {{$filter['name']}}
                            @if($filtersActive->keys()->contains($filter['id']))
                                    : {{$filtersActive[$filter['id']][0]}}
                                @if(count($filtersActive[$filter['id']])>1)
                                + {{count($filtersActive[$filter['id']])-1}}
                                @endIf
                            @endIf
                    </span>
                </span>
                    @if($filtersActive->keys()->contains($filter['id']))
                        <span class="button button_right button_s multifilter__reset"></span>
                    @endif
                    <div class="dropdown multifilter_sectioned">
                        <div class="multifilter-list">
                            <div class="multifilter-list__section">
                                <div class="multifilter-list__section_header">
                                   {{$filter['name']}}
                                </div>
                                <div class="multifilter-list multifilter-list__section_list" id="{{$filter['id']}}">
                                    @foreach($filter['values'] as $value)
                                        <div class="multifilter-item">
                                           <label class="multifilter-item__label @if($filtersActive->keys()->contains($filter['id']) && in_array( $value, $filtersActive[$filter['id']])) checked @endif"
                                                   val="{{$value}}"><input class="checkbox" type="checkbox" >{{$value}}
                                           </label>
                                        </div>
                                    @endforeach
                                </div>
                            </div>
                        </div>
                            <div class="multifilter-actions">
                                <span class="button button_s button_blue w-100 text-center multifilter-actions__apply" onclick="applyFilters()">Применить</span>
                            </div>
                        </div>
                    </div>
                </div>
            @endif
        @endforeach

Jquery to push filters to HTTP query (F**k u AJAX, simple STR_replace does fine)

I've used JSON.stringify to easily retrieve filters at controller

function applyFilters() {
        if ($('.multifilter-list.multifilter-list__section_list').find('.multifilter-item__label.checked').length) {
            let filters = {};
            $('.multifilter-list.multifilter-list__section_list').each(function () {
                let name =  $(this).attr('id');
                let values = [];
                    $(this).find('.multifilter-item__label.checked').each(function () {
                        values.push($(this).attr('val'));
                    });
                if (values.length > 0){
                    filters[name] = values;
                }
            });
        let link = '{{$request->FullUrlWithQuery(['filters' => ''])}}';
        link = link.replace('filters=','filters=' + JSON.stringify(filters));
        window.location.href = link;
        }
    }

Now we have to apply them to our products and its done

if ($request->filters){
           $products = $this->applyFilters($request->filters, $products);
       }

 public function applyFilters($filters, $products){
        $filters = json_decode(stripslashes($filters));
        foreach ($filters as $id => $values) {
                $products = $products->whereHas('properties', function ($query) use ($id, $values){
                    $query->where('id', $id)->whereIn('property_value', $values);
                });
        }
        return $products;
    }

Works absolutely fine, keeps pagination alive, and new filters are generated based on previous iteration, which means, if user selects "blue" in filter, and there is no blue coats, he wont be able to select "coats" in filter and get 0 results;

Activity icon

Replied to Laravel Fully Dynamic Catalog Filter

Problem solved. I think i shall leave my way to do it for somebody who wants to check out how its done.

Properties are in hasMany relation with products with property value pivot

Here is the code of function that retrieves them from database based on products builder. Works fast enough even for 5k products and 15 possible properties.

$properties = $products->flatMap(function ($item){
          return $item->properties->where('filter', 1);
        })->groupBy('id');
        $properties = $properties->map(function ($item){
            $item = $item->unique('pivot.property_value');
            return [
                'id' => $item->first()->id,
                'name' => $item->first()->name,
                'values' => $item->pluck('pivot.property_value')
            ];
        });

View code to display form. $filtersActive is simple collect(json_decode($request->filters)) at controller

@foreach($filters['properties'] as $filter)
        @if(count($filter['values']) > 1 || $filtersActive->keys()->contains($filter['id']))
            <div class="multifilter multifilter_material @if($filtersActive->keys()->contains($filter['id'])) multifilter_active @endIf">
                <div class="button-dropdown">
                <span class="button button_left button_s arrow-bottom multifilter__button">
                    <span class="multifilter__title">
                        {{$filter['name']}}
                            @if($filtersActive->keys()->contains($filter['id']))
                                    : {{$filtersActive[$filter['id']][0]}}
                                @if(count($filtersActive[$filter['id']])>1)
                                + {{count($filtersActive[$filter['id']])-1}}
                                @endIf
                            @endIf
                    </span>
                </span>
                    @if($filtersActive->keys()->contains($filter['id']))
                        <span class="button button_right button_s multifilter__reset"></span>
                    @endif
                    <div class="dropdown multifilter_sectioned">
                        <div class="multifilter-list">
                            <div class="multifilter-list__section">
                                <div class="multifilter-list__section_header">
                                   {{$filter['name']}}
                                </div>
                                <div class="multifilter-list multifilter-list__section_list" id="{{$filter['id']}}">
                                    @foreach($filter['values'] as $value)
                                        <div class="multifilter-item">
                                           <label class="multifilter-item__label @if($filtersActive->keys()->contains($filter['id']) && in_array( $value, $filtersActive[$filter['id']])) checked @endif"
                                                   val="{{$value}}"><input class="checkbox" type="checkbox" >{{$value}}
                                           </label>
                                        </div>
                                    @endforeach
                                </div>
                            </div>
                        </div>
                            <div class="multifilter-actions">
                                <span class="button button_s button_blue w-100 text-center multifilter-actions__apply" onclick="applyFilters()">Применить</span>
                            </div>
                        </div>
                    </div>
                </div>
            @endif
        @endforeach

Jquery to push filters to HTTP query (F**k u AJAX, simple STR_replace does fine)

I've used JSON.stringify to easily retrieve filters at controller

function applyFilters() {
        if ($('.multifilter-list.multifilter-list__section_list').find('.multifilter-item__label.checked').length) {
            let filters = {};
            $('.multifilter-list.multifilter-list__section_list').each(function () {
                let name =  $(this).attr('id');
                let values = [];
                    $(this).find('.multifilter-item__label.checked').each(function () {
                        values.push($(this).attr('val'));
                    });
                if (values.length > 0){
                    filters[name] = values;
                }
            });
        let link = '{{$request->FullUrlWithQuery(['filters' => ''])}}';
        link = link.replace('filters=','filters=' + JSON.stringify(filters));
        window.location.href = link;
        }
    }

Now we have to apply them to our products and its done

if ($request->filters){
           $products = $this->applyFilters($request->filters, $products);
       }

 public function applyFilters($filters, $products){
        $filters = json_decode(stripslashes($filters));
        foreach ($filters as $id => $values) {
                $products = $products->whereHas('properties', function ($query) use ($id, $values){
                    $query->where('id', $id)->whereIn('property_value', $values);
                });
        }
        return $products;
    }

Works absolutely fine, keeps pagination alive, and new filters are generated based on previous iteration, which means, if user selects "blue" in filter, and there is no blue coats, he wont be able to select "coats" in filter and get 0 results;

Activity icon

Replied to Laravel Fully Dynamic Catalog Filter

@prasadchinwal5 Thanks for the reply. I've seen it already never like it through

Activity icon

Started a new Conversation Laravel Fully Dynamic Catalog Filter

Hello there. My current quest is to make multifilter for e shop.

I found out how to generate filter form and collect data from it by js

I'm able to generate a JS object for my filters, im also able to succesfully pass it to controller by AJAX

This works fine, but i stuck and have no idea what to do next...

I've tried to use FullUrlWithQuery() function, but it generates a query for controller URL and i need it for current catalog url

I dont ask for code, just give me a direction, where to dig