php - undefined variable 。拉维

标签 php laravel eloquent

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

$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,它正在返回数组。这里有什么问题?谁能帮帮我?

最佳答案

尝试在 function($q) 之后添加 use($destination):

$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 。拉维,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48740075/

相关文章:

php - 国际字符排序规则(西类牙语),php

php - laravel 选择与每个帖子相关的最后一条评论

Laravel 缓存永远记住

php - 尝试在 Laravel Blade 上显示值时出现 undefined variable X 问题

php - 在 Laravel 5 注销后防止浏览器的后退按钮登录

php - Laravel 5 whereHas 链接和 orWhere

php - 如何使用 Eloquent 方式获得行位置

php - 队列 Redis 不会在 Laravel Forge 上被触发

php - 拉维尔 5.3 : Use auth/middleware on custom error page

php - 尝试使用 SQL Convert 函数缩短存储的 Datetime 值