Search code examples
laravellaravel-5.3

Laravel display a custom message in Maintenance Mode


I'm checking out Laravel docs for Maintenance Mode:

https://laravel.com/docs/5.3/configuration#maintenance-mode

When you execute the command php artisan down, it will put the application under maintenance mode, and return the 503.blade.php view.

Works good, but there is an option I can't really make work.. when I do:

php artisan down --message='Upgrading Database' --retry=60

I want to display the message in the view, I tried accessing the obvious choice with {{ $message }} without success, returns undefined variable.

My question is: how to access it?


Solution

  • By default 503.blade.php view doesn't use this message.

    This message is available in a JSON formatted file named storage/framework/down generated by php artisan down command.

    You could do something like this to access the directly the message in your view:

    {{ json_decode(file_get_contents(storage_path('framework/down')), true)['message'] }}
    

    A cleaner way is to use the $exception variable and include in your view {{ $exception->getMessage() }} like suggested in this answer.

    Under the hood, the CheckForMaintanceMode middleware reads the message and other data from the file and thrown a MaintanceModeException with this data.

    Edit: After Laravel 8, the payload that creates the storage/framework/down command has changed and doesn't include the exception message. You should use the {{ $exception->getMessage() }} instead on Laravel 8+.