2

I have set the debug to true on .env file. Added the exceptions correctly but when i am passing invalid or not exist in my database its showing me 404 error but here I put the custom error handling value. Here is my code. (also I put "Use Expectation;" on top so no need of \Expectation)

public function show($id) { //only one author with id try { $event = Event::with('eventCategory')->findOrFail($id); return new EventsResource($event); //return one author } catch(Expectation $e) { report($e); return response()->json(['status'=> false, 'message'=>'invalid data'],200); } } 
3
  • Does this answer your question? laravel routing and 404 error Commented Nov 14, 2019 at 6:32
  • In catch block its Exception not Expectation Commented Nov 14, 2019 at 6:41
  • You can use } catch (\Exception $ex) { I will suggest better to use if you are on PHP7 } (\Throwable $ex) { Commented Nov 14, 2019 at 6:42

2 Answers 2

4

Do you mean Exception? Because in your question it's Expectation...

Sign up to request clarification or add additional context in comments.

Comments

2

As @user3532758 is implying, you probably want to be catching the base Exception not Expectation.

Also make sure you are referencing Exception from the root namespace, assuming the code you have shown is in a Controller:

try { ... } catch (\Exception $e) { ... } 

PHP Manual - Classes - Exception

1 Comment

I am really sorry!! Its a silly mistake. I am new to laravel but thats not the mater i should have to check properly.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.