Home > Software engineering >  Laravel Filter Query results based on URL Parameters
Laravel Filter Query results based on URL Parameters

Time:12-11

The issue:

How do I pass the following values to the URL. It would also need to have the ability to pass both, the date option and category to the URL, so I can handle it in the controller with the $request->query('') I cannot figure out how to do this?

I have the following Controller:

public function allCoursesList(Request $request)
{
    if (empty($request->query())) {
        return view('pages.all-events', [
            'events' => Events::query()
                ->where('start', '>', now())
                // add pagination
                ->paginate(15)

        ]);
    } else {
        return view('pages.all-events', [
            'events' => Events::query()
                ->where('category', $request->query('category'))
                ->where('start', '>', now())
                // add pagination
                ->paginate(15)

        ]);
    }
}

and I have my filter blade:

<form >
    <div >
        <div >
            <div>
                <div >Kategorie</div>
                <select >
                    <option value="0">Lorem Impsum</option>
                    <option value="1">Lorem Impsum</option>
                    <option value="2">Lorem Impsum</option>
                    <option value="3">Lorem Impsum</option>
                    <option value="4">Lorem Impsum</option>
                    <option value="5">Lorem Impsum</option>
                    <option value="6">Lorem Impsum</option>
                    <option value="7">Lorem Impsum</option>
                    <option value="8">Lorem Impsum</option>
                </select>
            </div>
            <div>
                <div >Zeitbereich</div>
                <select >
                    <option>Anstehend</option>
                    <option>Heute</option>
                    <option>Morgen</option>
                    <option>Diese Woche</option>
                    <option>Dieses Wochenende</option>
                    <option>Nächste Woche</option>
                    <option>Nächster Monat</option>
                </select>
            </div>
        </div>
        <x-button type="submit" >Suchen</x-button>
    </div>
</form>

based on the controller I can pass my category to the query with e.g. example.com/loremimpsum?category=1

CodePudding user response:

This issue was caused because the <select> was missing a name value. So by adding name="category"

<select name="category" >
    <option value="0">Lorem Impsum</option>
    <option value="1">Lorem Impsum</option>
    <option value="2">Lorem Impsum</option>
    <option value="3">Lorem Impsum</option>
    <option value="4">Lorem Impsum</option>
    <option value="5">Lorem Impsum</option>
    <option value="6">Lorem Impsum</option>
    <option value="7">Lorem Impsum</option>
    <option value="8">Lorem Impsum</option>
</select>

I get the wanted URL parameter for the controller. Thank you very much for this info @lagbox

  • Related