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


laravel Passing multiple variables in to a controller view

我正在尝试获取url,例如localhost/cat/restaurants/this-is-test,但在尝试加载url时出错。

我有两个数据库表"评论"answers"类别"

reviews-id,cat_id,slug,categorys-id,cat

我有一个模型,它通过cat_id 链接两个表

SQLSTATE[42S22]:未找到列:1054"where clause"中的未知列"cat"(SQL:select*from reviews where cat=Restaurants and slug=this-is-test)

我猜它试图从评论表中提取"cat"而不是"categorys",但在我的刀片模板中,我有url route-{!!url::route('reviews.slug',[$review->categorys->cat,$review->slug])!},它提取两个变量并加载url localhost/cat/restaurants/this-is-测试

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();