Skip to content
Advertisement

how to append `request()->input()` to GET form action

I have a filtration page to get the posts according to user choices, I want to give the user the ability to choose multiple ways to filter, such as the price, post date, category, …etc. therefore I should append the user’s previous choices.

for example ex: example.com?category=1 and now the user wants to filter with price, it should be. ex: example.com?category=1&price=200

it works for me while trying <a></a> tag, ex <a href="{{ route('posts', array_merge(request()->input(), ['price' => 200])) }}">price 200</a>.

so I need to do the same with form GET, here’s how I tries to solve it:

{!! Form::open(
[
    'route'     => [Request::route()->getName(), request()->input()],
    'method'    => 'GET'
]) !!}
    @foreach ($specialisms as $specialism)
    <div class="custom-control custom-checkbox">
        {!! Form::checkbox('specialism', $specialism->id, request('specialism') == $specialism->id, ['class' => 'custom-control-input', 'id' => Str::snake($specialism->title['en']), 'onchange' => '$(this).parent().parent().submit()']) !!}
        <label class="custom-control-label" for="{{ Str::snake($specialism->title['en']) }}">{{ $specialism->lang_title }}</label>
    </div>
    @endforeach
{!! Form::close() !!}

but it doesn’t work, it always removes all parameters and append-only the parameter for the form input

Advertisement

Answer

you should modify:

checkbox name :specialism[] and condition: in_array($specialism->id, request('specialism'))

to be :

{!! Form::open([
'route' => [Request::route()->getName(), request()->input()],
'method' => 'GET',
]) !!}
@foreach ($specialisms as $specialism)
    <div class="custom-control custom-checkbox">
        {!! Form::checkbox('specialism[]', $specialism->id, in_array($specialism->id, request('specialism')), ['class'
        => 'custom-control-input', 'id' => Str::snake($specialism->title['en']), 'onchange' =>
        '$(this).parent().parent().submit()']) !!}
        <label class="custom-control-label"
            for="{{ Str::snake($specialism->title['en']) }}">{{ $specialism->lang_title }}</label>
    </div>
@endforeach
{!! Form::close() !!}

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement