SlimDeluxe's avatar

Seeder automatically generating model's relationship methods?

I have a Filament app and whenever I run the seeder, it generates unwanted relationship methods in models.

Here are the two models:

This is the migration:

Now, when running php artisan migrate:fresh, there are no problems. But when running php artisan migrate:fresh --seed, the two model classes are changed. They each automatically get the relationship method:

Site:

    /** @return HasMany<\Eclipse\Cms\Models\Section, self> */
    public function sections(): HasMany
    {
        return $this->hasMany(\Eclipse\Cms\Models\Section::class);
    }

Section:

    /** @return BelongsTo<\Eclipse\Core\Models\Site, self> */
    public function site(): BelongsTo
    {
        return $this->belongsTo(\Eclipse\Core\Models\Site::class);
    }

This is not desirable, since the Site class/table is parent to almost every other model, to allow multi-tenancy separation. Also it's mostly non-sensical and unpractical (e.g. there's no way you'd ever access logs with $site->logs()).

I've tested a clean Laravel 12 app and it does not do this, so I must have added a package that's causing this. I've already excluded PhpStorm and Laravel Idea plugin (same happens without them).

Before I go any deeper, has anyone come across such a thing? LLM muchachos are not helping.

PS: Forgot to mention, in the migration, even if I use the commented line instead of the proper foreign ID spec, it still generates the methods, as long as the column name is "site_id", but if I use "site", the methods are not generated. That is wild :)

0 likes
3 replies
LaryAI's avatar
Level 58

This is a very unusual and surprising behavior—Laravel itself never auto-generates relationship methods in your model classes, even when running seeders. The only way your model files would be modified is if some code or package is explicitly writing to your model files during the seeding process.

Possible Causes

  1. Third-party Package

    • Some package you installed is scanning your database schema and generating relationship methods in your models. This is not standard Laravel behavior.
    • Packages that do this are rare, but some code generators (for example, reliese/laravel, krlove/eloquent-model-generator, or similar) can generate or update model files based on your schema.
  2. Custom Seeder/Command

    • Check your DatabaseSeeder or any custom seeders for code that might be calling a model generator.
    • Look for any artisan commands or scripts that run as part of your seeding process.
  3. Composer Scripts or Post-Actions

    • Check your composer.json for any "post-autoload-dump" or similar scripts that might trigger model generation.

How to Diagnose

  1. Check for Model Generator Packages

    • Run composer show and look for packages like reliese/laravel, krlove/eloquent-model-generator, or anything with "generator" in the name.
  2. Search Your Codebase

    • Search for sections() and site() in your codebase to see if any code is writing these methods into your models.
    • Search for file_put_contents, fwrite, or similar functions in your project.
  3. Check Seeder and Migration Files

    • Review your DatabaseSeeder.php and any custom seeders for code that might trigger model generation.
  4. Check for Custom Artisan Commands

    • Look in app/Console/Commands for any commands that might be called during seeding.

How to Fix

  • Remove or Configure the Offending Package:
    If you find a package like reliese/laravel or similar, either remove it or configure it so it does not auto-generate relationships, or only runs when you explicitly call it.

  • Remove Generator Calls from Seeders:
    If your seeder or a post-migrate hook is calling a generator, remove that call.

  • Restore Your Models:
    Revert your model files to their original state and commit them to version control to avoid accidental overwrites.


Example: Reliese Laravel

If you have reliese/laravel installed, it can generate relationships in your models. By default, it does not run during php artisan migrate:fresh --seed, but if you have a custom command or script that triggers it, it could.

Check for code like:

php artisan code:models

Or in PHP:

\Artisan::call('code:models');

If you find this, remove or restrict it.


Summary

Laravel does not auto-generate relationship methods.
A package or custom code is doing this.
Audit your dependencies and seeders for model generators.

If you post your composer.json and DatabaseSeeder.php, I can help you pinpoint the culprit!

SlimDeluxe's avatar
SlimDeluxe
OP
Best Answer
Level 4

I solved this, it was the Filament Shield plugin that was generating the methods. It's a feature of the plugin, although I have no idea for what purpose. The problem was I have this in the app seeder:

        Artisan::call('shield:generate', [
            '--all' => null,
            '--panel' => 'admin',
            '--option' => 'permissions',
            '--minimal' => null,
        ]);

... which generates the relationships too. To stop it, AFAIK one has to create a seeder with shield:seeder instead of calling the shield:generate on each seeder execution.

Please or to participate in this conversation.