laravel 将多个变量传递到控制器视图

laravel Passing multiple variables in to a controller view

我正在尝试获取 url 例如 localhost/cat/restaurants/this-is-a-test 但在尝试加载 url.

时它会抛出错误

我有 2 个分贝 tables 'reviews' 和 'categorys'

评论 - id,cat_id,slug,类别 - id,cat

我有一个通过 cat_id

链接两个 table 的模型

SQLSTATE[42S22]:未找到列:1054 'where clause' 中的未知列 'cat'(SQL:select * 来自 cat 的评论= 餐馆和鼻涕虫 = this-is-a-test)

我猜它试图从评论 table 中提取 'cat' 而不是 'categorys' 但是在我的 blade 模板中我有 url 路线 - {!! URL::route('reviews.slug',[$review->categorys->cat,$review->slug] ) !!} 它会拉取两个变量并加载 url localhost/cat/restaurants/this-is-a-test

routes.php

Route::get('reviews/cat/{cat}/{slug}', ['as' => 'reviews.slug', 'uses' => 'ReviewController@ShowbySlug']); 

reviewcontroller.php

public function ShowBySlug($cat,$slug) {
$slugs = Review::with('reviewimages','maps','categorys')->where('cat',   $cat)->where('slug', $slug)
         ->get();
return View::make('pages.reviews')
       ->with('slugs', $slugs)
       ->with('cat', $cat)
       ;

}

您可以使用 whereHas 按关系过滤:

$slugs = Review::with('reviewimages','maps','categorys')
     ->whereHas('categorys', function($q) use ($cat){
         $q->where('cat', $cat);
     })
     ->where('slug', $slug)
     ->get();