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

Bartude's avatar

Notifications Factory For Users

Is there any way to create a notifications factory for my notifications table in the database? I found a file called Factory in Illuminate\Contracts\Notifications, but without an example I'm not really sure how to proceed or even if this file has anything to do with what I want.

0 likes
2 replies
martinbean's avatar

I'm not really sure how to proceed or even if this file has anything to do with what I want.

@Bartude Might help if you tell us what it is you’re wanting to do?

Punksolid's avatar

I think it is better to execute manually the notification in your tests, but if you want to generate a lot of dummy data then the factory will help. To do so

Your NotificationFactory.php

<?php

/* @var $factory \Illuminate\Database\Eloquent\Factory */

use Faker\Generator as Faker;

$factory->define(Illuminate\Notifications\DatabaseNotification::class, function (Faker $faker) {
    return [
                'id' => $faker->uuid
    ];
});

When you are going to generate it


        $notification = factory(DatabaseNotification::class)->create([
            "type" => "Namespace\ClassNameOfNotification",
            "notifiable_type" => "Notifiable\Model",
            "notifiable_id" => random_int(8888, 9999), // id of the notifiable model
            "data" => [
                "any"=> "value"
            ]
        ]);

Obviously you could pass any of the values to your factory but if you want to generate it for a real model you will need to pass it in your create.

Please or to participate in this conversation.