How To Create Multiple Laravel Models With A Single Command
Generate multiple models at once using Powershell and the Artisan console

Search for a command to run...
Generate multiple models at once using Powershell and the Artisan console

No comments yet. Be the first to comment.
Deploying Laravel applications can be a tedious and time-consuming process, but with the use of continuous integration and continuous deployment (CI/CD) pipelines, the process can be automated. Azure DevOps is a popular choice for creating such pipel...

Let's talk generating .gitignore files - a file every git repo should have. Some tools for scaffolding projects like create-tauri-app or composer create-project initialize .gitignore for you. But what about applications which do not use these? Like w...

This article will cover basic steps for creating a time-triggered Lambda function. This can be useful if you want to check for something periodically (for example once per hour). You might want to check if your server is up, fetch data from an API an...

In this article, I will be going through the process of Laravel application deployment, namely to Google Cloud Platform (GCP). This article will cover the basics of GCP as a Cloud Service Provider, the steps of setting up a new Virtual Machine via th...

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.
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.
The script works as follows:
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.
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.