How To Create Multiple Laravel Models With A Single Command

Generate multiple models at once using Powershell and the Artisan console

Have you ever encountered the need to create multiple artisan objects, like Eloquent models, controllers or Livewire components at one moment? When starting a new project, this is a very likely situation. Solving it by running command after command like php artisan make:model $NAME -msc over and over again isn't a particularly good idea, in my opinion, as it takes a lot of time. As far as I know, there aren't any command-line tools to remediate this problem, so I decided to make one.

Solving the problem

The obvious solution to this is to take a OS-native shell, like Bash for Linux or Powershell for Windows (although you can use PowerShell on Linux as well), and write a script to automate the tedious task of running the aforementioned artisan commands by hand. As I was on Windows at the time of getting the idea of this script, I'm going to present the solution to the problem in Powershell. Note that it can also be easily converted to a Linux shell script and I'm surely going to do that some time in the future.

This implementation of the script contains a solution to batch Eloquent model creation with additional (optional) parameters like migrations, controllers, seeders and factories connected to the model. Let's take a look at the final script:

Note: The full version of this script is also available at my GitHub.

# artisan-model-generator.ps1

if (!(Test-Path ".\artisan" -PathType Leaf)) {
  echo "ERROR: Artisan not found in this directory"
  exit
}

$input = Read-Host -Prompt "Enter model names separated by commas"

if (!$input) {
  echo "ERROR: No model names entered"
  exit
}

echo "Enter switches to create additional classes (like -msfc)"
$switch = Read-Host -Prompt "Enter the desired switches"

if (!$switch) {
  echo "WARNING: No switch selected"
} else {
  if ($switch -notcontains "-") {
    $switch = "-" + $switch
  }
  if ($switch -notmatch "[mscf]") {
    echo "ERROR: The switch can contain only [mscf] characters"
    exit
  }
}

$input = $input -replace '\s',''
$switch = $switch -replace '\s',''
$models = $input.Split(",")

foreach ($model in $models) {
  echo "Creating model $model"
  php artisan make:model $model $switch
}

The snippet above is a simplified version of the script you can get in the GitHub repo linked above it.

What the script does

The script works as follows:

  1. It checks if the artisan script is present, if it isn't, the script exists
  2. It prompts the user for input, in this case, the names of the models the user wants to create
  3. If the input is valid, it prompts the user for additional components to create
  4. If input is empty, only the model is created, otherwise, the script takes the entered input as artisan make:model switches. It also checks if the entered switches are valid
  5. After this, white spaces are removed from the input strings and the models array is created from the first input string (model names)

After these steps are complete, the script starts to generate our models with the desired names and switches. The output of the command would look like this:

PS C:\Users\domin\Documents\www\laravel-test> .\artisan-models.ps1
Artisan model generator
Enter model names separated by commas: Person, Article, Item
Enter the desired switches: ms
Creating model Person
Model created successfully.
Created Migration: 2021_03_31_112410_create_people_table
Seeder created successfully.
Creating model Article
Model created successfully.
Created Migration: 2021_03_31_112527_create_articles_table
Seeder created successfully.
Creating model Item
Model created successfully.
Created Migration: 2021_03_31_112533_create_items_table
Seeder created successfully.

In the snippet above, we're creating 3 models - Person, Article and Item. With these, we are also creating a migration and a seeder. The artisan command php artisan make:model $ITEM -ms is run by the script and we've saved some time. It's a win-win situation.

Wrapping up

This has been just a quick article about a script I've made a couple of days back, just to show you how easy it can be to batch generate a bunch of Eloquent models. Of course, this script can be taken a bit further, maybe with better exception handling or by expanding it beyond just generating models. If you'd like to expand upon this basis, feel free to fork the aforementioned repo and submit a pull request.

No Comments Yet