P
P
Prodion2019-12-28 23:11:01
Laravel
Prodion, 2019-12-28 23:11:01

How to filter by parameters through the Eager Load closure?

I need to get all colors if there are no parameters, and if there are parameters, get the colors of a certain collection or a certain manufacturer.

public function index(Request $request)
{
    $colors = Color::with(['collection' => function($query){
        $query->with('manufacturer');
    }])->get();

    if ($request->has('manufacturer')) {
        # code...
    }

    if ($request->has('collection')) {
        # code...
    }

    return view('colors.index', compact('colors'));
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question