Call to undefined method firstOrFail

Asked by: frankl79
Date:
Viewed: 522
Answers: 1
  • 0

Hi,

I’m getting this error with Laravel when I try to access a page on my blog.

Call to undefined method IlluminateDatabaseQueryBuilder::firstOrFail()

This is the query I changed:

$category = DB::table('blog_post_categories')->where('slug', $slug)->firstOrFail();

What am I doing wrong?

 

Answers

Answer by: ChristianKovats

Answered on: 20 Jul 2023

  • 0

You cannot use the firstOrFail method with the query builder. You need to use it with Eloquent model.

Try this

$category = BlogCategory::where('slug', $slug)->firstOrFail();

 

Change the model name if I didn’t get it right.

Please log in to post an answer!