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
-
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.
-
Custom Seeder/Command
- Check your
DatabaseSeederor 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.
- Check your
-
Composer Scripts or Post-Actions
- Check your
composer.jsonfor any"post-autoload-dump"or similar scripts that might trigger model generation.
- Check your
How to Diagnose
-
Check for Model Generator Packages
- Run
composer showand look for packages likereliese/laravel,krlove/eloquent-model-generator, or anything with "generator" in the name.
- Run
-
Search Your Codebase
- Search for
sections()andsite()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.
- Search for
-
Check Seeder and Migration Files
- Review your
DatabaseSeeder.phpand any custom seeders for code that might trigger model generation.
- Review your
-
Check for Custom Artisan Commands
- Look in
app/Console/Commandsfor any commands that might be called during seeding.
- Look in
How to Fix
-
Remove or Configure the Offending Package:
If you find a package likereliese/laravelor 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!