Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined variable. Laravel

I am trying to filter my results. For this i have following query:

$destinations = $request['destinations'];
        if($request->has('destinations'))
        {
            $trips = Trip::where('status','=',1)->whereHas('destinations', function($q) {
            $q->where('destinationsid', '=', $destinations);
            })->get();
        }else{
             echo "No destination provided";
        }

But I'm getting undefined variable $destinations. If i replace $destinations with any id (6) it shows results. I have also tried to echo $destinations outside the query, it is returning array. What is wrong here? Can anyone help me?

like image 628
Anon Avatar asked Jan 28 '26 06:01

Anon


1 Answers

Try to add the use($destination) after function($q):

$destinations = $request['destinations'];

if($request->has('destinations'))
{
     $trips = Trip::where('status','=',1)->whereHas('destinations', function($q) use($destinations) {
        $q->where('destinationsid', '=', $destinations);
     })->get();
} else {
        echo "No destination provided";
}

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!