Ghulam Farid
Ghulam Farid

Reputation: 131

How to create multiple models in Laravel 8 using single artisan command?

I'm struggling to create more than one models (I don't want to run command every time to create a lot of models) using artisan command in Laravel 8 but it's giving me error.

What I tried is

php artisan make:model Photo Staff Product

The error I faced,

Too many arguments to "make:model" command, expected arguments "name".

Upvotes: 0

Views: 6168

Answers (3)

Khaled Sayed
Khaled Sayed

Reputation: 29

you can prepare your artisan commands in a separate text file like a photo attached with this post select all and copy them then past all in the ide terminal and they will run all enter image description here

Upvotes: -1

Ghulam Farid
Ghulam Farid

Reputation: 131

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

Upvotes: 0

Giles Bennett
Giles Bennett

Reputation: 1636

As the error message suggests, it only expects one parameter, which is the name of the one, single, model you are trying to create. You cannot create multiple models in one artisan command.

If your terminal allows it, the up key will return you back to the previous command typed in, speeding up the process of generating models.

Upvotes: 1

Related Questions