tom harrison
tom harrison

Reputation: 3433

laravel Passing multiple variables in to a controller view

I am trying to get the url e.g localhost/cat/restaurants/this-is-a-test but it throws an error when attempting to load the url.

I have 2 db tables 'reviews' and 'categorys'

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

I have a model that links both tables by cat_id

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'cat' in 'where clause' (SQL: select * from reviews where cat = Restaurants and slug = this-is-a-test)

I'm guessing it is trying to pull 'cat' from the reviews table instead of 'categorys' however in my blade template I have the url route - {!! URL::route('reviews.slug',[$review->categorys->cat,$review->slug] ) !!} and it pulls both variables and loads the 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)
       ;

}

Upvotes: 0

Views: 1258

Answers (1)

lukasgeiter
lukasgeiter

Reputation: 153120

You can use whereHas to filter by a relationship:

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

Upvotes: 2

Related Questions