Decoding Laravel 5.5 : report()

laravel report

Taylor Otwell – founder of Laravel recently announced about the new helper function report() that is going to be available from Laravel 5.5. This function helps you to report the exception without stopping the request. And Today, I’m going to explain how this function works under the hood. Lets get started.

 

Error Reporting

 
I’m running Laravel 5.5 dev version with fresh database and tables migrated. Now, we will create a sample route like the following.
 

 

Here, since I have not seeded my tables, this is going to throw ModelNotFoundException. Under catch block we are going to report it to Laravel by calling report() method. This will in return call report() method on App\Exception\Handlers.php class where we will write our logic for handling this exception.

 

 

In order to understand how this works, we have to go report function definition.

 

Introducting Laravel Report

 
report() is defined on Illuminate\Foundation\helpers.php file.

 

 

Here, report calls the Illuminate\Contracts\Debug\ExceptionHandler contract class. Now, since this is just a contract, we have to figure out the class that is binded with this contract. Lets open the app.php file under bootstrap folder.

 

 

On app.php you can find that Illuminate\Contracts\Debug\ExceptionHandler is binded with App\Exceptions\Handler.php. This way when report() helper function calls app(ExceptionHandler::class), it will return App\Exceptions\Handler class and from there report() is called.

This is how the new laravel report function works!

Happy Coding!


I'm a full stack freelance PHP developer. I have worked on various PHP frameworks like Laravel, Code Igniter, Magento, Wordpress.