Categories Games

New User Registration – Example of “Dummy” Data Seed – Quick Admin Panel


Often in projects, new users have to see sample data, in order to understand what the system actually does, and how to use it. How to implement “fake data” seeding in Laravel, on every new user registration?

We will have an example task calendar mini project, based on the QuickAdminPanel “Tasks+Calendar” module, but the same code can be applied to any Laravel project.

Every time a new user signs up, they will be taken to their calendar, with 10 examples of “fake” tasks already featured, for the next 5 days.


Step 1. Factory Task

First, we create a Factory class to define dummy data for creating tasks.

database/factory/TaskFactory.php:


use App\Task;
use App\TaskStatus;
use Faker\Generator as Faker;

$factory->define(Task::class, function (Faker $faker) {
    return [
        'name'          => $faker->sentence(3),
        'description'   => $faker->sentence,
        'status_id'     => TaskStatus::inRandomOrder()->first(),
    ];
});

In general, ours app/Tasks.php The module has the following fillable fields:


class Task extends Model 
{
    // ...

    protected $fillable = [
        'name',
        'description',
        'status_id',
        'due_date',
        'created_by_id',
    ];

So, if you notice, Factory doesn’t fill in two columns deadlines And created_by_idand we will fill it in manually.


Step 2. Register Event Listeners and Seeders

Every time someone registers, an event is called Registeredand you can add anything called Listener class to it.

So, first, we create a class that will fire every time the event occurs.

app/Listeners/SeedTestDataForNewUser.php:


namespace App\Listeners;

use App\Task;
use Carbon\CarbonPeriod;
use Illuminate\Auth\Events\Registered;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class SeedTestDataForNewUser
{
    /**
     * Handle the event.
     *
     * @param  Registered  $event
     * @return void
     */
    public function handle(Registered $event)
    {
        $period = CarbonPeriod::create(now(), now()->addDays(5));

        foreach($period as $date)
        {
            factory(Task::class, 2)->create([
                'due_date'      => $date->format('Y-m-d'),
                'created_by_id' => $event->user->id,
            ]);
        }
    }
}

As you can see, there is only one method handle() (which is activated automatically), and in it we perform the following actions:

– Create a Carbon Period for the next five days
– For each of those days – we create 2 tasks with a factory, adding those two missing fields
– We use $event->user as a user parameter derived from the event.

The last thing we need is to attach that listener to the event.

We went to app/Provider/EventServiceProvider.php and add two lines (bold):


namespace App\Providers;

use App\Listeners\SeedTestDataForNewUser;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;

class EventServiceProvider extends ServiceProvider
{

    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
            SeedTestDataForNewUser::class,
        ],
    ];

    // ...

}

As you can see, there is already one listener for email verification upon registration, so we just have to add our own class to the same event.

Just that!
Repository for this entire project: LaravelDaily/Laravel-New-User-Event-Seeder



Teknologi Terkini

Agen Togel Terpercaya

Bandar Togel

Sabung Ayam Online

Berita Terkini

Artikel Terbaru

Berita Terbaru

Penerbangan

Berita Politik

Berita Politik

Software

Software Download

Download Aplikasi

Berita Terkini

News

Jasa PBN

Jasa Artikel

News

Breaking News

Berita

More From Author