0

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".
2
  • 1
    The docs 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? Commented Dec 8, 2021 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. Commented Dec 9, 2021 at 9:12

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

-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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.