Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

trifek's avatar

Event planner - how to design?

hi, I want to prepare an application for planning my duties (notes). Each note will consist of:

  • title of the note,
  • description of the note,
  • date the note was added,
  • notifications*

Notifications can be: a) one-time, on a selected day. b) cyclical every X days, c) cyclical every X months.

I create simple model for this task:

class CreateEventsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('events', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->mediumText('description')->nullable();
            $table->timestamp('date')->nullable();
        });
    }

    public function down()
    {
        Schema::dropIfExists('events');
    }
}

But I have no idea about these notifications. Notification = sending an email reminder about the event on the day and 3 days before the event.

Anyone have an idea of the easiest way to do this?

Please help.

0 likes
1 reply
LaryAI's avatar
Level 58

To implement notifications for your event planner application, you can make use of Laravel's built-in notification system along with the Laravel Scheduler.

Here's a step-by-step solution:

  1. Add the necessary columns to your events table migration to store the notification details. Modify your migration file as follows:
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateEventsTable extends Migration
{
    public function up()
    {
        Schema::create('events', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->mediumText('description')->nullable();
            $table->timestamp('date')->nullable();
            $table->timestamp('notification_date')->nullable();
            $table->timestamp('notification_sent_at')->nullable();
        });
    }

    public function down()
    {
        Schema::dropIfExists('events');
    }
}
  1. Create a new notification class using the make:notification Artisan command:
php artisan make:notification EventReminder

This will generate a new notification class EventReminder in the app/Notifications directory.

  1. Open the EventReminder notification class and modify the toMail method to define the email content:
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class EventReminder extends Notification
{
    use Queueable;

    public function via($notifiable)
    {
        return ['mail'];
    }

    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->subject('Event Reminder')
            ->line('You have an upcoming event: ' . $notifiable->title)
            ->line('Event Description: ' . $notifiable->description)
            ->line('Event Date: ' . $notifiable->date);
    }
}
  1. In your Event model, add the Illuminate\Notifications\Notifiable trait and define a routeNotificationForMail method to specify the email address to which the notification should be sent:
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;

class Event extends Model
{
    use Notifiable;

    // ...

    public function routeNotificationForMail()
    {
        return $this->email; // Replace with the actual email column name
    }
}
  1. Create a new command to send the notifications. Run the following command to generate the command class:
php artisan make:command SendEventReminders

This will create a new command class SendEventReminders in the app/Console/Commands directory.

  1. Open the SendEventReminders command class and modify the handle method to fetch the events that require notifications and send the notifications:
use App\Models\Event;
use App\Notifications\EventReminder;
use Illuminate\Console\Command;

class SendEventReminders extends Command
{
    protected $signature = 'reminders:send';

    protected $description = 'Send event reminders';

    public function handle()
    {
        $events = Event::whereNotNull('notification_date')
            ->whereNull('notification_sent_at')
            ->where('notification_date', '<=', now())
            ->get();

        foreach ($events as $event) {
            $event->notify(new EventReminder);
            $event->update(['notification_sent_at' => now()]);
        }

        $this->info('Event reminders sent successfully.');
    }
}
  1. Register the SendEventReminders command in the app/Console/Kernel.php file by adding it to the commands array:
protected $commands = [
    // ...
    \App\Console\Commands\SendEventReminders::class,
];
  1. Finally, schedule the SendEventReminders command to run at the desired intervals. Open the app/Console/Kernel.php file and add the following code to the schedule method:
protected function schedule(Schedule $schedule)
{
    $schedule->command('reminders:send')->daily();
}

This will run the SendEventReminders command once every day. You can modify the schedule as per your requirements.

That's it! Now, whenever an event's notification_date is reached, the SendEventReminders command will send an email notification using the EventReminder notification class.

Remember to run php artisan migrate to apply the changes to your database schema.

Note: Make sure you have properly configured your mail driver in the .env file to enable email notifications.

Please or to participate in this conversation.