PHP code example of rboonzaijer / laravel-multiple-flash-messages

1. Go to this page and download the library: Download rboonzaijer/laravel-multiple-flash-messages library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rboonzaijer / laravel-multiple-flash-messages example snippets


class UserController
{
    public function store()
    {
        // ...

        flash('User created');

        flashWarning('User has no permissions yet');

        return to_route(...);
    }

flash('Flagged', [
    'type' => 'danger-flagged',
    'description' => 'You have been flagged for creating to many users',
    'details' => [
        'ticket' => 123,
        'urls' => [
            'https://example.com',
            'http://example.com',
        ]
    ]
]);

flash($message, $options, $flashToSession);
flashInfo($message, $options, $flashToSession);
flashSuccess($message, $options, $flashToSession);
flashWarning($message, $options, $flashToSession);
flashError($message, $options, $flashToSession);

flash('Saved', [], Auth::user()->hasNotificationsEnabled());

flash('Saved', [], false);

// ...

if(Auth::user()->hasNotificationsEnabled()) {
    flashMessagesToSession();
}

// app/Http/Middleware/HandleInertiaRequests.php

public function share(Request $request): array
{
    return array_merge(parent::share($request), [
        'messages' => fn () => session()->get('messages'),
    ]);
}
blade
{{-- /resources/views/layouts/app.blade.php --}}

@
blade
{{-- /resources/views/partials/flash-messages/container.blade.php --}}

@if(session()->has('messages'))
    <div class="flash-messages">
        @foreach(session()->get('messages') as $index => $message)
            @
blade
{{-- /resources/views/partials/flash-messages/message.blade.php --}}

<div class="{{ $message['type'] }}" id="flash-message-{{ $index }}">
    <span class="title">
        {{ $message['message'] }}

        @if(isset($message['details']['ticket']))
            <span>- Ticket #{{ $message['details']['ticket'] }}</span>
        @endif
    </span>

    @if(isset($message['description']))
        <p class="description">
            {{ $message['description'] }}
        </p>
    @endif

    @if(isset($message['details']['urls']))
        <ul class="urls">
            @foreach($message['details']['urls'] as $index => $url)
                <li>
                    <a href="{{ $url }}">
                        {{ $url }}
                    </a>
                </li>
            @endforeach
        </ul>
    @endif
</div>