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

skeith22's avatar

Method checker on Models if seeding command is running.

Does anyone know how to check on the public static function root method of a Model if the seeding command is running?

Do the Models have a built-in function or method checker if seeders are running?

0 likes
9 replies
aurawindsurfing's avatar

Hey @skeith22

This feels a bit hacky but you might try that:

if (file_exists(base_path('database/seeds/' . class_basename() . 'Seeder.php')))
    {
        dd('File exists.');
    } else
    {
        dd('File does not exist.');
    }
skeith22's avatar

hey, thanks for the reply but what I mean is a METHOD checker on Models if the seeding command is running. Not if the Model seeder file exists.

aurawindsurfing's avatar

Running or exists?

If running then you can simply plug into models creating event.

click's avatar

I think you want to know if the current model is that is being saved is populated from the seeder? Am I correct?

Why do you want to know this? What are you trying to achieve? Maybe there is another solution to what you want to do.

skeith22's avatar

hey click, I have a function on the creating event on the public static function boot of the Models. I don't want the creating event being event being triggered if the seeding command is running.

like this one

/**
 * Run functions on boot.
 *
 */
public static function boot()
{
    parent::boot();

    static::creating(function ($model) {
        // If seeding command is running RETURN

        // Otherwise do something
    });
}
1 like
aurawindsurfing's avatar

Hey @skeith22

I think I just learned something ;-) There are 2 ways to achive what you are looking for:

  1. Use this instead of standard factory() helper: factoryWithoutObservers() this will not fire any events for creating your objects.

  2. Disable observers for all test that you run if it is testing that you are after. In your setUp() method of your Testcase that you always extend: Model::unsetEventDispatcher();

Hope it helps!

click's avatar

@skeith22 you could try @aurawindsurfing 's solution if that fits your needs. Otherwise I'm afraid there is not a build in solution right now to "know" that the seeder is currently saving those models.

Another little bit hacky approach is to manually set a global variable in your seeder and than check for that variable.

I don't have time to test it but something like below could do the trick. But if you can do it the "official" way by simply not firing the events I would go with that.

class Seeder 
{
    protected static $running = false;

    public function isRunning()
    {
        return static::$running;
    }

    public function start()
    {
        static::$running = true;
    }

}

and than in your DatabaseSeeder class:

public function run()
{
    Seeder::start();
    /* your seeder coder */
}

And in your model

static::creating(function ($model) {
    if (!Seeder::isRunning()) {
     // do something only when the seeder is not running
    }
})

@fyi, the static method is called boot() not root()

2 likes
skeith22's avatar

apparently, the factoryWithoutObservers() helper doesn't exist though. Where did you find it?

Please or to participate in this conversation.