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

dhiman252's avatar

Automatically generate relations Eloquent

Is there any way we can create relations automatically while we create a model using artisan command ?

0 likes
2 replies
bobbybouwmann's avatar

Nope you can't at the moment. You can however create your own command for it ;)

cristianllanos's avatar

If you are using MySQL and Laravel 5.1 or above you can use php artisan code:models from this package reliese/laravel. All you need to do is:

  1. composer require reliese/laravel
  2. Add the service provider to your config/app.php file Reliese\Coders\CodersServiceProvider::class
  3. Publish the config file with php artisan vendor:publish --tag=reliese-models
  4. Make sure your database is correctly configured in config/database.php and .env files.
  5. And finally issue the command: php artisan code:models

This package will scan your database and create all models for you including their relations. If you need something more specific, you can customize its config file.

Hope this helps :)

1 like

Please or to participate in this conversation.