A package to flash multiple messages using Laravels default session message flashing system

Overview

Flash multiple advanced messages with both text, messages and links

bilfeldt/laravel-flash-message

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

An opinionated solution for flashing multiple advanced messages from the backend and showing these on the frontend using prebuild customizable Tailwind CSS alert blade components.

Installation

Install the package via composer and you are ready to add messages and show these on the frontend.

composer require bilfeldt/laravel-flash-message

Optional: In case you wish to use the message flashing feature allowing messages to be made available on the next request (usefull in combination with redirects) simply add the ShareMessagesFromSession middleware to the web group defined in app/Http/Kernel.php just after the ShareErrorsFromSession middleware:

// app/Http/Kernel.php

/**
 * The application's route middleware groups.
 *
 * @var array
 */
protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Laravel\Jetstream\Http\Middleware\AuthenticateSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \Bilfeldt\LaravelFlashMessage\Http\Middleware\ShareMessagesFromSession::class, // <------ ADDED HERE
        \App\Http\Middleware\VerifyCsrfToken::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],
    ...

Usage

The most basic usage of this package is creating a message inside a controller and passing it to the view:

<?php

namespace App\Http\Controllers;

use Bilfeldt\LaravelFlashMessage\Message;
use Illuminate\Http\Request;

class PostController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @param  Request  $request
     */
    public function index(Request $request)
    {
        $message = Message::warning('This is a simple message intended for you') // message/success/info/warning/error
            ->title('This is important')
            ->addMessage('account', 'There is 10 days left of your free trial')
            ->link('Read more', 'https://example.com/signup');
            
        return view('posts')->withMessage($message);
    }
}

Redirects

Sometimes a redirect is returned to the user instead of a view. In that case the message must be flashed to the session so that they are available on the subsequent request. This is simply done by flashing the $message:

<?php

namespace App\Http\Controllers;

use Bilfeldt\LaravelFlashMessage\Message;
use Illuminate\Http\Request;

class PostController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @param  Request  $request
     */
    public function index(Request $request)
    {
        $message = Message::warning('This is a simple message intended for you') // message/success/info/warning/error
            ->title('This is important')
            ->addMessage('account', 'There is 10 days left of your free trial')
            ->link('Read more', 'https://example.com/signup');
            
        return redirect('/posts')->withMessage($message); // This will flash the message to the Laravel session
    }
}

Note that for this to work you will need to add the ShareMessagesFromSession middleware to app/Http/Kernel.php as described in the installation section above.

In the situations where we need to flash a message to session without access to a redirect, like in a Laravel Livewire component, then we have added a small helper method session_message($message) which does the same.

Adding message from anywhere in the code

Messages can be adding bacially anywhere in the codebase using the View facade. Although most usecases will be adding the messages from the controller, then this feature can be really powerful for example conjunction with middlewares:

\Illuminate\Support\Facades\View::withMessage($message);

Show messages

Once messages have been passed to the frontend these can be shown by simply using the following component in any view file:

<x-flash-alert-messages />

The above will display messages from the default bag. Displaying messages from a specific bag is done by simply providing the name of the bag:

<x-flash-alert-messages bag="demo" />

Multiple message bags

There might be situations where it can be usefull to have multiple "MessagebBags" (the same approach as Laravel usese for the validation messages) and in this case one can name the bag like so:

return view('posts')->withMessage($message, 'bag-name');
// or 
return redirect('/posts')->withMessage($message, 'bag-name');

and when displaying the messages simply pass the bag name as well:

<x-flash-alert-messages bag='bag-name' />

Tip

You might have a layout where you would always like to flash the messages above the main content or just below the title. You can simply add the <x-flash-message.alert /> to your layout file in the place you would like the messages to show and that way you do not need to call this in multiple views.

If you then need to show specific messages at a specific location simply use a [named message bag](#Multiple message bags) for these messages and show them at the desired location

Alternatives / Supplements

This package is useful when working with blade files and passing messages from the backend to the frontend during rendering. If you are looking for toast (popup) messages instead have a look at the awesome package usernotnull/tall-toasts for the TALL stack.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.

Comments
  • New `withGlobalErrors` macro

    New `withGlobalErrors` macro

    There is an existing view method withErrors that is used like this:

    view('post-index')
        ->withErrors(['field' => 'Some error'], 'default');
    

    but the problem is that the errors are available as $errors only in the provided view, meaning two things:

    1. They override the global $errors used by validation and the method with same name withErrors() on redirects
    2. Any parent/child views do not have access to these errors (like for example the main layout)

    I wrote a PR for this that was unfortunately rejected - see https://github.com/laravel/framework/pull/39459

    So this issue is about creating a macro withGlobalErrors that merges the errors to the global errors:

    view('post-index')
        ->withGlobalErrors(['field' => 'Some error'], 'default'); // These messages are merged into the 'default' bag
    

    This is how to merge: https://laravel.com/api/8.x/Illuminate/Support/MessageBag.html#method_merge

    opened by bilfeldt 2
  • Show error component only if validation errors exists and add formatt…

    Show error component only if validation errors exists and add formatt…

    • [x] Fix issue that an empty alert blade was shown even if no validation errors were present
    • [x] Remove messages from errors blade component
    • [x] Add formatting option to the errors blade component
    opened by bilfeldt 0
  • Adjust text colors

    Adjust text colors

    The text colors on the current blade components are:

    • message: text-gray-800 / text-gray-700
    • info: text-blue-700 / text-blue-600
    • success: text-green-700 / text-green-600
    • warning: text-orange-700 / text-orange-600
    • alert: text-red-700 / text-red-600
    Screenshot 2022-12-16 at 00 24 28

    But they should be changed to a black / greyish like so (probably text-gray-800 for all components).

    Screenshot 2022-12-16 at 13 06 08 Screenshot 2022-12-16 at 13 05 59
    opened by bilfeldt 0
  • Namespace components

    Namespace components

    All components are currently prefixed with flash like

    <x-flash-alert ... />
    

    but we should use vendor namespacing like

    <x-flash::alert ... />`
    

    which is done by registering the components using componentNamespace() which is not supported by the spatie/laravel-package-tools package.

    opened by bilfeldt 0
  • Add individual blade components

    Add individual blade components

    This PR adds dedicated blade components for use like so:

    <x-flash-alert-message
        title="Test title"
        text="test text"
    />
    
    <x-flash-alert-info
        title="Test title"
        text="test text"
    />
    
    <x-flash-alert-success
        title="Test title"
        text="test text"
    />
    
    <x-flash-alert-warning
        title="Test title"
        text="test text"
    />
    
    <x-flash-alert-error
        title="Test title"
        text="test text"
    />
    
    opened by bilfeldt 0
  • Restyle blade components

    Restyle blade components

    • [x] Implement blade component message (grey)
    • [x] Implement blade component info (blue)
    • [x] Implement blade component success (green)
    • [x] Implement blade component warning (yellow)
    • [x] Implement blade component error (red)
    • [x] Modify blade component alert to dynamically use the correct massage above (to not break)

    We should get inspiration from the Shopify banners: https://polaris.shopify.com/components/banner

    Do you mind assisting @comes?

    opened by bilfeldt 0
  • PHP Warn for countable interface

    PHP Warn for countable interface

    Return type of Bilfeldt\LaravelFlashMessage\ViewFlashMessageBag::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /Users/andersbilfeldt/github.com/eesea/enigma/vendor/bilfeldt/laravel-flash-message/src/ViewFlashMessageBag.php on line 108
    
    opened by bilfeldt 0
Releases(v1.0.1)
  • v1.0.1(Dec 23, 2022)

    What's Changed

    • Update FlashMessageServiceProvider.php by @bilfeldt in https://github.com/bilfeldt/laravel-flash-message/pull/29

    Full Changelog: https://github.com/bilfeldt/laravel-flash-message/compare/v1.0.0...v1.0.1

    Source code(tar.gz)
    Source code(zip)
  • v1.0.0(Dec 18, 2022)

    What's Changed

    • Apply fixes from StyleCI by @bilfeldt in https://github.com/bilfeldt/laravel-flash-message/pull/5
    • Add slot to alert components by @bilfeldt in https://github.com/bilfeldt/laravel-flash-message/pull/14
    • Add tests by @bilfeldt in https://github.com/bilfeldt/laravel-flash-message/pull/15
    • Rename the flash-alert-messages component by @bilfeldt in https://github.com/bilfeldt/laravel-flash-message/pull/16
    • Fix/php version 8 by @bilfeldt in https://github.com/bilfeldt/laravel-flash-message/pull/22
    • Set blade text colors to grey by @bilfeldt in https://github.com/bilfeldt/laravel-flash-message/pull/23
    • Use namespace for components instead of prefix by @bilfeldt in https://github.com/bilfeldt/laravel-flash-message/pull/24
    • add dark mode by @comes in https://github.com/bilfeldt/laravel-flash-message/pull/25
    • Add blade component for showing $errors by @bilfeldt in https://github.com/bilfeldt/laravel-flash-message/pull/26
    • Show error component only if validation errors exists and add formatt… by @bilfeldt in https://github.com/bilfeldt/laravel-flash-message/pull/27
    • Create new method 'withGlobalErrors' by @bilfeldt in https://github.com/bilfeldt/laravel-flash-message/pull/2
    • Remove the spatie package skeleton and fix issues with component name.. by @bilfeldt in https://github.com/bilfeldt/laravel-flash-message/pull/28

    Full Changelog: https://github.com/bilfeldt/laravel-flash-message/compare/v0.4.0...v1.0.0

    Breaking changes

    Blade component are now namespaced instead of prefixed. This means that usage of the components should be changed from the old prefix syntax

    <x-flash-alert ... /> // no longer supported
    

    to the new namespace syntax

    <x-flash::alert ... /> // new syntax
    
    Source code(tar.gz)
    Source code(zip)
  • v0.4.0(Dec 16, 2022)

    What's Changed

    • Add individual blade components by @bilfeldt in https://github.com/bilfeldt/laravel-flash-message/pull/13

    Full Changelog: https://github.com/bilfeldt/laravel-flash-message/compare/v0.3.1...v0.4.0

    Source code(tar.gz)
    Source code(zip)
  • v0.3.1(Dec 16, 2022)

  • v0.3.0(Dec 15, 2022)

    What's Changed

    • Add return types by @bilfeldt in https://github.com/bilfeldt/laravel-flash-message/pull/10
    • Update blade component styling #11 by @comes in https://github.com/bilfeldt/laravel-flash-message/pull/12

    New Contributors

    • @comes made their first contribution in https://github.com/bilfeldt/laravel-flash-message/pull/12

    Full Changelog: https://github.com/bilfeldt/laravel-flash-message/compare/v0.2.1...v0.3.0

    Source code(tar.gz)
    Source code(zip)
  • v0.2.1(Mar 28, 2022)

    What's Changed

    • Fix missing messages were doing redirect by @bilfeldt in https://github.com/bilfeldt/laravel-flash-message/pull/8

    Full Changelog: https://github.com/bilfeldt/laravel-flash-message/compare/v0.2.0...v0.2.1

    Source code(tar.gz)
    Source code(zip)
  • v0.2.0(Feb 20, 2022)

    What's Changed

    • Apply fixes from StyleCI by @bilfeldt in https://github.com/bilfeldt/laravel-flash-message/pull/3
    • Apply fixes from StyleCI by @bilfeldt in https://github.com/bilfeldt/laravel-flash-message/pull/4
    • Add Laravel9 support by @bilfeldt in https://github.com/bilfeldt/laravel-flash-message/pull/7

    Full Changelog: https://github.com/bilfeldt/laravel-flash-message/compare/v0.1.0...v0.2.0

    Source code(tar.gz)
    Source code(zip)
  • v0.1.0(Nov 2, 2021)

Owner
Bilfeldt
Bilfeldt
Textpattern-default-theme - Textpattern CMS default theme.

Textpattern CMS default theme This project is the source for the default theme that ships as standard with Textpattern CMS. It is intended as a starti

Textpattern CMS 61 Nov 21, 2022
Adds a way to write php and run it directly in Laravels' Artisan Tinker.

Adds a way to write php in PhpStorm/IDEA and run it directly as if through laravel artisan tinker - allowing you to quickly run a piece of code with a

Robbin 120 Jan 2, 2023
🔔 Flasher is a powerful and flexible flash notification system for PHP, Laravel, Symfony

A powerful and flexible flash notifications system for PHP, Laravel, Symfony ?? PHP Flasher helps you to add flash notifications to your PHP projects.

PHP Flasher 158 Jan 4, 2023
A helper package to flash a bootstrap alert to the browser via a Facade or a helper function.

Alert Box (Laravel) A helper package to flash a bootstrap alert to the browser via a Facade or a helper function. <div class="alert alert-info fade in

Ben-Piet O'Callaghan 17 Dec 30, 2022
Laravel 5 Flash Notifications with icons and animations and with a timeout

Notify (Laravel) Notify alert boxes to the browser with sound and font awesome icons and give it a timeout to fly out. Works great to notify the user

Ben-Piet O'Callaghan 31 Oct 8, 2022
Message box application written in Laravel - simple inbox design using auth UI.

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

null 0 Dec 25, 2021
PHP library for sending messages using a ntfy server.

ntfy-php-library PHP library for sending messages using a ntfy server. Supports ntfy server version 1.27.2. Install composer require verifiedjoseph/nt

Joseph 6 Dec 14, 2022
A bookmarkable, searchable cheatsheet for all of Laravel's default Artisan commands.

artisan.page A bookmarkable, searchable cheatsheet for all of Laravel's default Artisan commands. Generation The generation of the manifest files is d

James Brooks 284 Dec 25, 2022
This tool gives you the ability to set the default collapse state for Nova 4.0 menu items.

Nova Menu Collapsed This tool gives you the ability to set the default collapse state for Nova 4.0 menu items. Requirements php: >=8.0 laravel/nova: ^

Artem Stepanenko 10 Nov 17, 2022
Disque is a distributed message broker

Disque, an in-memory, distributed job queue Disque is an ongoing experiment to build a distributed, in-memory, message broker. Its goal is to capture

Salvatore Sanfilippo 7.9k Jan 7, 2023
Nachricht is an message dispatcher which focuses on distributing workloads

Nachricht Nachricht is a message dispatcher which focuses on distributing workloads. Features Directly dispatch messages Dispatch messages via AMQP au

JTL Software 1 Jan 18, 2022
Run multiple websites using the same Laravel installation while keeping tenant specific data separated for fully independent multi-domain setups, previously github.com/hyn/multi-tenant

The unobtrusive Laravel package that makes your app multi tenant. Serving multiple websites, each with one or more hostnames from the same codebase. B

Tenancy 2.4k Jan 3, 2023
Run multiple websites using the same Laravel installation while keeping tenant specific data separated for fully independent multi-domain setups.

Tenancy for Laravel Enabling awesome Software as a Service with the Laravel framework. This is the successor of hyn/multi-tenant. Feel free to show su

Tenancy 1.1k Dec 30, 2022
An artisan command for using multiple environment configurations in Laravel 5

Laravel 5 env An artisan command for managing multiple .env of your Laravel 5 app. Installation Copy EnvCommand.php to app/Console/Commands/ of your L

Tommy Ku 18 Nov 29, 2022
Run multiple websites using the same Laravel installation while keeping tenant specific data separated for fully independent multi-domain setups, previously

Run multiple websites using the same Laravel installation while keeping tenant specific data separated for fully independent multi-domain setups, previously

Tenancy 2.4k Jan 3, 2023
Laravel Messages (Contact us) with attachment.

Laravel Messages (Contact us) with attachment. Documentation You can find the detailed documentation here in Laravel Messages Documentation. Contribut

Pharaonic 5 Sep 12, 2022
An online communication application that provides a real-time or live transmission of text messages from sender to receiver.

Realtime-chat-application An online communication application that provides a real-time or live transmission of text messages from sender to receiver.

isha 2 Aug 15, 2022
Laravel Custom Response Messages from Passport Oauth2 Server

This is a sample repository showing how to install Oauth2 server using Laravel's passport package and customize its responses.

M. Ismail 7 Nov 22, 2022
One-to-one plugin for editing world chat messages.

WorldChat One-to-one plugin for editing world chat messages. Supports English and Turkish language To set a new world chat format /worldchat new "worl

Aydın Demirci 12 May 20, 2023