我正在尝试过滤我的结果。为此,我有以下查询:

$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";
        }

但是我得到了 undefined variable $destinations。如果我用任何 id (6) 替换 $destinations,它会显示结果。
我还尝试在查询之外回显 $destinations,它正在返回数组。这里有什么问题?谁能帮我?

最佳答案

尝试在 use($destination) 之后添加 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";
}

关于php - undefined variable 。 Laravel,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48740075/

10-12 13:21