Home > front end >  Laravel 8 - How to use where conditions for relation's column
Laravel 8 - How to use where conditions for relation's column

Time:12-04

hello friends can you help me to get the contents of $filter? I want to run where which is where the target column is in the relation array

    $filter = $request->get('Biogear');
    $data = DetailBarang::with(['barang' => function ($q) use ($filter) {
        $q->where('brand', '=', $filter);
    }])->get();
    return response()->json($data);

CodePudding user response:

you can try this

    $filter = $request->get('Biogear');
    $data = DetailBarang::with('barang')->whereHas('barang',function (Illuminate\Database\Eloquent\Builder $q) use ($filter) {
        $q->where('brand', '=', $filter);
    })->get();

    return response()->json($data);
  • Related