2

How can I return json data when laravel catch exception? I'd like to return Json data when the data does not exist on database.

When laravel found the record from database, it returns correct json data.Yeah! If laravel have fail to search any record, it doesn't give json data! laravel just redireted the page which shows "Whoops, looks like something went wrong." and give soem extra information, "ModelNotFoundException".

Following code is what I tried.

    public function show($id)
    {
            try {
                    $statusCode = 200;
                    $response = [
                            'todo' => []
                    ];

                    $todo = Todo::findOrFail($id);

                    $response['todo']= [
                            'id'       =>       $todo->id,
                            'title'    =>       $todo->title,
                            'body'     =>       $todo->body,
                    ]; 

            } catch(Exception $e) {
                    // I think laravel doesn't go through following exception
                    $statusCode = 404;   
                    $response = [
                            "error" => "You do not have that record"
                    ];

            } finally {
                    return response($response, $statusCode);

            }
   }
2
  • Try to catch exception like this: catch(Illuminate\Database\Eloquent\ModelNotFoundException $e) Commented Feb 20, 2015 at 1:26
  • I tried, but it didn't catch anything. So I changed it to if condition. I know it's not perfect, but at least it return json...I will try the idea you given. There might be some exception class...I guess.... Commented Feb 20, 2015 at 2:11

1 Answer 1

1

I solve the problem.. First, I changed findOrFail method to find method. Second, I realized Exception and Illuminate\Database\Eloquent\ModelNotFoundException $e couldn't catch anything. So I changed to if condition. Then it works.

    public function show($id)
    {        
            $statusCode = 200;
            $response = [
                    'todo' => []
            ];

            $todo = Todo::find($id);

            if ( is_null($todo) ) {
                    $statusCode = 404;
                    $response = [
                            "error" => "The record doesn't exist"
                    ];

            } else {
                    $response['todo']= [
                            'id'       =>       $todo->id,
                            'title'    =>       $todo->title,
                            'body'     =>       $todo->body,
                    ]; 

            }

            return response($response, $statusCode);
    }
Sign up to request clarification or add additional context in comments.

1 Comment

"if ( is_null($todo) ) {" i would write it as "if (!$todo instanceOf Todo ) {" for more accuracy :)

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.