B
B
BarneyGumble2018-08-17 20:12:06
Laravel
BarneyGumble, 2018-08-17 20:12:06

How to fill checkboxes regarding get-parameters in the address bar in Laravel?

I am developing an online store on Laravel. I am currently working on the implementation of the product filter.
It is necessary, having a string with search parameters, to fill in the checkboxes in the form that performs filtering.
Those. now I have this line:
localhost:3000/?category=tops&color=black
And I want the checkboxes to be filled like this on page load:

<form action="{{ route('layouts.main') }}" method="GET">

<h4>Категория</h4>
<label>
    <input type="checkbox" checked="" name="category[]" value="tops">
    Топы
</label>
<label>
    <input type="checkbox" name="category[]" value="bottoms">
    Нижнее бельё
</label>

<h4>Цвет</h4>
<label>
    <input type="checkbox" checked="" name="category[]" value="black">
    Чёрный
</label>
<label>
    <input type="checkbox" name="category[]" value="white">
    Белый
</label>

</form>

This is necessary for me so that I do not lose the value of the form by following the specified link with parameters.
What is the best way to do this?
There was an idea in the controller to do something like:
if($request->category){
    $categories = $request->category;
}

if($request->color){
    $colors = $request->color;
}

return view('layouts.main')->with([
            'categories' => $categories,
            'colors' => $colors
        ]);

But it is not clear how then in the blade template to put checked on certain checkboxes

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin B., 2018-08-17
@BarneyGumble

For example like this
Better yet, use the extension for forms, then it will set the desired value itself if it comes with a request
https://laravelcollective.com/docs/5.4/html#checkb...
Form::checkbox('name', 'value', true);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question