Search code examples
laraveleloquentlaravel-10

filter data using whereHas in laravel 10 not working


My program is like this =

$listreq2 = OutboundRequest::with('outboundmaterialrequest')
->whereIn('id', $outboundIds)
->whereHas('outboundmaterialrequest', function ($query) use ($ids) {
    $query->whereIn('id', $ids);
})->get();

Why is my data filter on ->whereHas('outbound material request', function ($query) use ($ids) { doesn't work the contents of $ids for example are = 2,3

but all of the outboundmaterialrequest data appears


Solution

  • you have written wrong query, you are using with clause which will always return data and filter will not work in this case as you are expecting. Your query should be like this.

    
    $listreq2 = OutboundRequest::query()
        ->whereIn('id', $outboundIds)
        ->withWhereHas('outboundmaterialrequest', function ($query) use ($ids) {
            $query->whereIn('id', $ids);
        })->get();