【问题标题】:Laravel query builder not using variable from get methodLaravel 查询构建器不使用 get 方法中的变量
【发布时间】:2017-04-10 18:07:42
【问题描述】:

我的一个控制器中有这个功能。

public function tourList($country, $category)
{
    $tour = Tour::whereHas('country', function($q) {
                    $q->where('name','=', $country);
                })
                ->whereHas('category', function($r) {
                    $r->where('name','=', $category);
                })
                ->get();

    return view('tour-list.blade.php')->withTour('$tour');
}

虽然从 get 方法中传递了两个变量。但我得到了错误

Undefined variable: country

【问题讨论】:

  • 显示路线路径@zacharyDale
  • Route::get('tour/{country}/{categpry}', ['as' => 'tour.list', 'uses' => 'PublicController@tourList']);
  • 将 '$tour' 更改为 $tour,因为我们必须在 withTour 中传递变量。

标签: php laravel-5.3 laravel-query-builder


【解决方案1】:

您在匿名函数中缺少use,因此您的查询将如下:

$tour = Tour::whereHas('country', function($q) use($country) {
                $q->where('name','=', $country);
            })
            ->whereHas('category', function($r) use($category) {
                $r->where('name','=', $category);
            })
            ->get();

Docs

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-13
    • 2019-01-30
    • 2017-06-29
    • 1970-01-01
    • 2016-11-09
    • 2019-02-02
    • 2018-06-23
    • 2020-12-22
    相关资源
    最近更新 更多