jeudi 26 juillet 2018

Laravel 5.4: Filter and meet all arguments from request

I need your help. Still learning a lot about Laravel and been searching the whole web for a solid answer to this question. I'm trying to get a collection of products that meet all the given request arguments. So:

?brand=adidas,nike&gender=women

Will get me all the Adidas & Nike shoes for Women.

I've got this so far:

$brandRequest = $request->brand;
$brandReqArray = explode(',', $brandRequest);

$genderRequest = $request->gender;
$genderReqArray = explode(',', $genderRequest);

$products = Product::with('brands', 'genders')
->whereHas('brands', function($query) use ($brandReqArray)  {
    $query->whereIn('slug', $brandReqArray);
})
->orWhereHas('genders', function($query) use ($genderReqArray)  {
    $query->whereIn('slug', $genderReqArray);
})->paginate(24);

I'm getting all the Adidas & Nike shoes and I'm getting all the women shoes. But if I'm trying both it shows nothing.

Can someone point me in the right direction or maybe even has a solution for this?

Thanks! :)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire