davism24's avatar

Laravel Filtering Results using Checkbox Selectors

Hi all,

I’m looking for some guidance on this issue I’ve been trying to fix for a few weeks now. I haven’t been able to find any videos on Laracasts that seem to address my issue but I just joined so if you can think of one please feel free to list it.

Background:

My website houses a set of games and each game has a set of tags. This is very similar to an ecommerce site and the same kind of an idea and you can filter on certain attributes.

Here’s a basic DB setup:

DB:

Game
----
ID
Title
Description

Tags
---
ID
Name
Enum (Genre, System, Warning, Other)

Front-End Checkboxes

<div class="grouped fields">
     @foreach($data['warnings'] as $warning)
           <div class="field">
                 <div class="ui checkbox">
                        <input type="checkbox" name="warninc[]" value="{{$warning->id}}">
                         <label>{{ $warning->name }}</label>
                 </div>
           </div>
     @endforeach
</div>
	

Now when filtering the game you can include or exclude multiple tags. This is a bit different but plays into some of the core features of the site. In addition, I want users to be able to bookmark the URL, so I’m passing this all through a get request. The return works fine but the front-end seems a bit messy.

I’ve run into two issues I seem to be struggling with how to solve.

(1) [Solution already found] I’m using checkboxes to allow multiple selection, I can’t seem to find anything within laravel to have them be rechecked on page reload after submit besides a jquery call on load to parse the URI and check them.

(2) My URL looks like game.test/search?warninc[]=3&warninc[]=7&rating[]=Mature&genreinc[]=8 is there a better way to do this?

Any assistance would be appreciated

0 likes
3 replies
guybrush_threepwood's avatar
Level 33

Hi @davism24

Won't this work to keep the checkboxes checked after submit?

<input
    type="checkbox"
    name="warninc[]"
    value="{{ $warning->id }}"
    @if(is_array(Request::get('warninc')) && in_array($warning->id, Request::get('warninc'))) 
        checked
    @endif
>

Regards

1 like
davism24's avatar

This worked! And such a simple solution. Thanks so much.

1 like

Please or to participate in this conversation.