-1

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".
Ghulam Farid
  • 470
  • 4
  • 10
  • 1
    [The docs](https://laravel.com/docs/8.x/eloquent#generating-model-classes) don't mention anything about creating multiple models, not sure why you thought that would work? Do you need to do this often? If yes, you could write a simple script. If not, is there any problem simply running the command once for each model? BTW - [please do not post images of code/error messages.](https://meta.stackoverflow.com/a/285557) – Don't Panic Dec 08 '21 at 21:46
  • Thank you for your suggestion. Well, I tried it my own, and there's no any description to create multiple models at once. So, I found the PowerShell script and share it here. – Ghulam Farid Dec 09 '21 at 09:12

3 Answers3

0

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.

Giles Bennett
  • 1,509
  • 1
  • 12
  • 15
0

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

Ghulam Farid
  • 470
  • 4
  • 10
-1

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