Quick Contact

    Error Handling with Laravel

    Here error handling is learned with php framework. So see how to work with error handling and how you can create your own exceptions inside the Laravel project. So first see how structure is setting up and next and last is how to create own exception if post is not found error display.

    – If talk about structure then if you have your app and exception then you have the exception handler in which there is a file named as “handler.php”, where you are working with the exceptions inside the level and if you have special exceptions you don’t need to be reported.

    – Now go to config file i.e. app.php and need to search for debug which will be ‘false’ by default. When you run in production it’s very important for you to show the errors because if you are showing errors in your website, you can show sensitive data and structure of block which is non-interested thing.

    So when you are running in production it will always be false.

    Good thing is when you go for new .env file then “APP_DEBUG = true” should be there so that’s little bit about working of structure.

    – Now if you go for the handler i.e. “handler.php” file in Exceptions folder in which function is there to be register as-:

    Now let’s go for application in browser and check how to go for good code.

    So here data don’t exist and showing the error.

    – Now write a code to show the output of 404 if any errors occur.

    – Go to PostController file here in http>controllers folder.

    If (! $post) {
         abort (404);


    Now let’s go back for the website and see what happens.

    So the code is working. So instead of this you have another way to write the code as-:

    – So Let’s do another thing- if you want 404 page differently then-:

    Now go to views and create new folder as “errors” and in that create new file in errors folder i.e. “404.blade.php” and write code as-:

    < h1> 404- not found < /h1>

    Custom Pages here!

    Now go back to browser and refresh the page so output is-:

    As it’s very important to capture about the exceptions if something is going wrong the you can send it up and you don’t need to check everything every times.

    So let’s go back to editor and go for controller folder and open postcontroller.php file. Write a code as-:

    $post = Posts::where ([‘id’ => $id])-> with (‘comments’)-> firstorFail();
    } catch (\Exception $exception) {
         Return view (‘error.post-not-found’);

    Another you have to do with this is go for new file in “errors” folder named as ‘post-not-found.blade.php’. In this file write code as-:

    < h1> Post not found < /h1>

    Your post in not found, try to select a second one!

    Now again go for browser and refresh the page so output is-:

    So catch the working. So the code which works as-:

    $post = Posts::where ([‘id’ => $id])-> with (‘comments’)-> firstorFail();
    } catch (\Exception $exception) {
         Return view (‘error.post-not-found’);

    So as in above code you are using “first or fail” that’s first offering will catch the error and catch the error will mean you will catch about the exceptions. If you don’t use the exceptions then you will just send it to the 404 page.

    – If you want to make more easy to catch the errors then you need to create your own exceptions. Go to command line shell and type-:

    abc-compose exec app php artisan make:exception PostNotFoundException

    Exception created successfully.

    NOW go to file in “exceptions folder” named as “postnotfoundexception.php”. Write the code is as-:


    More you have to add as> go back to postcontroller.php file and include as-:

    Use App\Exceptions\PostNotFoundException;

    And last thing you should do is add in catch formula as-:

    Catch (\Exception $exception) {
        Throw new PostNotFoundException();

    Code is as-:

    Go back to browser and see the result. So after reload the page you will get respective page you search for in url.


    Apply now for Advanced PHP Training Course

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.