php - Laravel Eloquent ORM whereHas and where in foreach -
i trying make following work, might bad practice, pretty new laravel, please let me know !
i have model named files
, linked keywords
through pivot table.
i running following code, undefined variable: keyword_id
error.
$keyword_ids = array(148, 4); $files = new files; foreach($keyword_ids $keyword_id) { $files = $files->wherehas('keywords', function($query) { $query->where('id', '=', $keyword_id); }); }
thanks lot !
you need use use
keyword have variables accessible inside closure.
foreach($keyword_ids $keyword_id) { $files = $files->wherehas('keywords', function($query) use ($keyword_id) { $query->where('id', '=', $keyword_id); }); }
Comments
Post a Comment