Met Laravel toon ik in allebei mijn projecten (kilometer registratie en maps) een bericht (Flash message) als er iets mis gaat. Het wordt met een code-library van Sweetalert gedaan. Dat ziet er vaak zo uit;

De onderstaande code staat bij mij, in een Laravel project, onder; AppHttp met de naam Flash. De code komt uit een les van Laracast.com en is aangepast door mij. Een website dat ik van harte aanraad!

<?php

namespace AppHttp;

/**
 * Class Flash
 *
 * @author Floris van Enter
 * @package AppHttp
 */
class Flash
{

    /**
     * Save info for info message
     *
     * @param $title
     * @param $message
     * @return Session /app/Http/flash::create
     */
    public function info($title, $message)
    {
        return $this->create($title, $message, 'info');
    }

    /**
     * * Save info for error message
     *
     * @param $title
     * @param $message
     * @return Session /app/Http/flash::create
     */
    public function error($title, $message)
    {
        return $this->create($title, $message, 'error');
    }

    /**
     * Save info for success message
     *
     * @param $title
     * @param $message
     * @return Session /app/Http/flash::create
     */
    public function success($title, $message)
    {
        return $this->create($title, $message, 'success');
    }

    /**
     * Save info for warning message
     *
     * @param $title
     * @param $message
     * @return Session /app/Http/flash::create
     */
    public function warning($title, $message)
    {
        return $this->create($title, $message, 'warning');
    }

    /**
     * Save info for overlay message optional with succes, info, error, warning
     *
     * @param $title
     * @param $message
     * @param string $level
     * @return Session /app/Http/flash::create
     */
    public function overlay($title, $message, $level = 'info')
    {
        return $this->create($title, $message, $level, 'flash_message_overlay');
    }

    /**
     * Save message info in session for execution
     * @param $title
     * @param $message
     * @param string $level
     * @param string $key
     * @return Session
     */
    public function create($title, $message, $level = 'info', $key = 'flash_message')
    {
        return session()->flash($key, [
            'title' => $title,
            'message' => $message,
            'level' => $level
        ]);

    }
}


/**
 * To successfully execute this code.
 *
 * A) At the following to your view;
 *
        @if (session()->has('flash_message'))
            <script type="text/javascript">
                swal({
                    title: '{{ session('flash_message.title') }}',
                    text:  '{{ session('flash_message.message') }}',
                    type:  '{{ session('flash_message.level') }}',
                    timer: 1700
                })
            </script>
        @endif

        @if (session()->has('flash_message_overlay'))
            <script type="text/javascript">
                swal({
                    title: '{{ session('flash_message_overlay.title') }}',
                    text:  '{{ session('flash_message_overlay.message') }}',
                    type:  '{{ session('flash_message_overlay.level') }}',
                    confirmButtonText: 'ok'
                })
            </script>
        @endif
 *
 * B) At the following to a helper file to help execute the model;
 *
        function flash($title = null, $message = null)
        {

            $flash = app('AppHttpFlash');

            if (func_num_args() == 0) {
                return $flash;
            }

        return $flash->info($title, $message);
        }
 *
 * C) And point to the following files (or merge them);
 *      1) sweetalert2.js
 *      2) sweetalert2.css
 *      3) Get them from; https://limonte.github.io/sweetalert2/
 *
 * D) Fire alert with;
 * flash()->error('Niet gevonden!', 'Route is niet gevonden.');
 * ** error can be warning, info, success overlay, etc.
 */