0

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

}

1 Answer 1

2

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.