Aditya Kappagantula
Aditya Kappagantula

Reputation: 562

Laravel 5.0 Model not found exception

try {
  $university_exists = University::where('name', '=', $university_name) - > firstOrFail();
} catch (ModelNotFoundException $e) {
  $title = "Error retrieving data";
  $message = "Entered university not found!";
  $success = 0;
  return Response::json(['title' => $title, 'message' => $message, 'success' => $success], 200);
}

The above code works when I give correct university name. When I give wrong university name it throws a model not found exception.

Which I try to catch in the catch block. However, it doesn't get caught.

Can someone help me please?

ModelNotFoundException in Builder.php line 151: No query results for model [App\University].

Upvotes: 2

Views: 3420

Answers (1)

lukasgeiter
lukasgeiter

Reputation: 152860

You probably have to import the exception class with:

use Illuminate\Database\Eloquent\ModelNotFoundException;

at the top of your file.

Upvotes: 5

Related Questions