How to send variable to wherehas function in Laravel

If you need to send variable to wherehas function , all you have to do it to use “use” .. here is an example…

To list the Users whom their posts are in certain langue .

$language = 3;
$users = Users::whereHas('posts', function ($query) use($language){
                    $query->where('language_id', $language);
                })->get();

Leave a Reply

Your email address will not be published. Required fields are marked *