We can do this using OS-native shell. We have to write the PowerShell
script to perform this tasks.
Here it is,
#checking if there is any artisan script present, if not then exit
if (!(Test-Path ".\artisan" -PathType Leaf)) {
echo "ERROR: Artisan not found in this directory"
exit
}
#promting user
$input = Read-Host -Prompt "Enter model names separated by commas"
if (!$input) {
echo "ERROR: No model names entered"
exit
}
else
{
$input = $input -replace '\s','' #removing white spaces if any
$input = $input -replace ',+',',' #removing more than 1 commas with single comma
#checking if input contains any special character using regex
if ( $input -match '[!@#\$%\^&\*\(\)\.\{\}\[\]\?\|\+\=\-\/]' ){
echo "ERROR: Incorrect model names";
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 -notmatch "-") {
$switch = "-" + $switch
}
if ($switch -notmatch "[mscf]") {
echo "ERROR: The switch can contain only [mscf] characters"
exit
}
}
$switch = $switch -replace '\s',''
#spliting the string
$models = $input.Split(",")
foreach ($model in $models) {
echo "Creating model $model"
php artisan make:model $model $switch
}
save the file using the .ps1
extension starting with name artisan (e.g. artisan-models.ps1
) and run directly using .\artisan-models.ps1
command.
Here's Link