Home

Awesome

📣 Dismissibles for Laravel

Dismissibles for Laravel

A Laravel package for easily managing the visibility of your recurring, dismissible objects like popups/notifications/modals on the backend. This package does not include frontend components, so it's compatible with any frontend you can use.

📕 Table of Contents

✅ What problem does this solve?

Say you have a popup you want to show to every user, daily for a week. Users can dismiss it and it should not show up again for the rest of the day until the next day.

This packages handles the complex logic regarding whether the (dismissible) popup should be visible to the current user at the current moment. It basically handles the visibility of your dismissible. It's highly customizable, making it very flexible for many scenario's.

Because it's serverside we can easily get statistics like who dismissed what, when and where.

📦 Installation

  1. Require the package in your Laravel application
composer require rellix/dismissibles-for-laravel
  1. Run the migrations to create the database tables
php artisan migrate

❓ How to use

1. Add the interface and trait to any model

use Rellix\Dismissibles\Contracts\Dismisser;
use Rellix\Dismissibles\Traits\HasDismissibles;

class User implements Dismisser
{
    use HasDismissibles;
    
    ...
}

2. Create a dismissible (migration)

use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\DB;

return new class () extends Migration {
    public function up(): void
    {
        DB::table('dismissibles')->insert([
            'name'         => 'Test Popup', // This is your **unique** identifier
            'active_from'  => Date::createFromFormat('d-m-Y', '01-03-2024'),
            'active_until' => null, // Optional end date
            'created_at'   => Date::now(),
            'updated_at'   => Date::now(),
        ]);
    }
};

and run your created migration:

php artisan migrate
<details> <summary>💡 You can also create/fetch a Dismissible inline using the "active"-scope and "firstOrCreate".</summary>
Dismissible::active()->firstOrCreate(
    ['name' => 'Test Popup'], 
    [
        'active_from'  => Date::createFromFormat('d-m-Y', '01-03-2024'),
        'active_until' => null,
        'created_at'   => Date::now(),
        'updated_at'   => Date::now(),
    ]
);
</details>

3. Check if it should be visible at the current moment

use Rellix\Dismissibles\Facades\Dismissibles;

$showPopup = Dismissibles::shouldBeVisible('Test Popup', $user);

// Here are some more examples, including ones with additional conditionals:
$showPopup = Dismissibles::shouldBeVisible('Happy New Year 2025 Popup', $user);
$showPopup = Dismissibles::shouldBeVisible('Newsletter signup modal', $user) && !$user->is_subscribed;
$showPopup = Dismissibles::shouldBeVisible('Complete your profile notification', $user) && !$user->has_completed_profile;
$showPopup = Dismissibles::shouldBeVisible('50% Off First Purchase Popup', $user) && !$user->has_orders;

// You can also get all Dismissibles in one query (performance) and use the model methods.
$dismissibles = Dismissibles::getAllFor($user);

<details> <summary>💡 You can also use the individual models.</summary>
use Rellix\Dismissibles\Facades\Dismissibles;

$popup = Dismissibles::get('Test Popup');

$showPopup = $popup->shouldBeVisibleTo($user);
</details>

4. Dismiss it for a specified period

use Rellix\Dismissibles\Facades\Dismissibles;

Dismissibles::dismiss('Test Popup', $user)->untilNextWeek();

// Here's an overview of all the ways you can dismiss:
Dismissibles::dismiss('Test Popup', $user)
    ->untilTomorrow();
    ->untilNextWeek();
    ->untilNextMonth();
    ->untilNextQuarter();
    ->untilNextYear();
    ->until($dateTime);
    ->forHours($numberOfHours);
    ->forDays($numberOfDays);
    ->forWeeks($numberOfWeeks);
    ->forMonths($numberOfMonths);
    ->forYears($numberOfYears);
    ->forever();
<details> <summary>💡 You can also use the individual models.</summary>
use Rellix\Dismissibles\Facades\Dismissibles;

$popup = Dismissibles::get('Test Popup');

// Here's an overview of all the ways you can dismiss:
$popup->dismissFor($user)
    ->untilTomorrow();
    ->untilNextWeek();
    ->untilNextMonth();
    ->untilNextQuarter();
    ->untilNextYear();
    ->until($dateTime);
    ->forHours($numberOfHours);
    ->forDays($numberOfDays);
    ->forWeeks($numberOfWeeks);
    ->forMonths($numberOfMonths);
    ->forYears($numberOfYears);
    ->forever();
</details>

❗ Good to know

💾 Database tables

The database structure allows you to easily track activity regarding dismissibles. Due to the extra_data column it's also very flexible!

dismissibles (popups, notifications, modals)

idnameactive_fromactive_untilcreated_atupdated_at
3Test Popup2024-03-01 00:00:00null2023-12-15 17:35:542023-12-15 17:35:54

dismissals (activity)

iddismissible_iddismisser_typedismisser_iddismissed_untilextra_datacreated_atupdated_at
153App\Models\User3282024-04-29 00:00:00"{"route":"home.index"}"2024-04-28 17:35:542024-04-28 17:35:54

☕ Buy me a coffee

If you like this package, consider buying me a coffee :-).