Filtering page results Laravel many to many, many ids to find one entry

  eloquent, filter, laravel, many-to-many

I have a page that displays all the records from the ‘Receita’ table like this:

public function mostrarTodos()
{
    $receitas = Receita::all()->toArray();
    $ingredientes = Ingrediente::all()->toArray();
    return view('home', compact('receitas', 'ingredientes'));
}

on the top of the page there is a form with many checkboxes that will filter the results displayed on the front page:

<div class="col-span-2 border p-2">
    <form action="filtro" class="grid grid-cols-4" method="GET">
        @foreach ($ingredientes as $ingrediente)
        <div>
            <label for="{{ $ingrediente['id'] }}">{{ $ingrediente['nome'] }}</label>
            <input type="checkbox" name="ingrediente[]" id="{{ $ingrediente['id'] }}" value="{{ $ingrediente['id'] }}">
        </div>
        @endforeach
        <button class="rounded p-2 bg-purple-200 col-span-4" type="submit">Filtrar</button>
    </form>
</div>

The question is, how can I use that form to filter the results that are displayed on the page since each entry can have one or many of those checkboxes? In other words, show me only the entries that have the checked checkboxes.

Source: Laravel

Leave a Reply