我正在努力使用 Laravel 8 中的 artisan 命令创建多个模型(我不想每次都运行命令来创建很多模型),但这给了我错误。
我尝试的是
php artisan make:model Photo Staff Product
我遇到的错误,
Too many arguments to "make:model" command, expected arguments "name".
我正在努力使用 Laravel 8 中的 artisan 命令创建多个模型(我不想每次都运行命令来创建很多模型),但这给了我错误。
我尝试的是
php artisan make:model Photo Staff Product
我遇到的错误,
Too many arguments to "make:model" command, expected arguments "name".
正如错误消息所暗示的,它只需要一个参数,即您尝试创建的单一模型的名称。您不能在一个工匠命令中创建多个模型。
如果您的终端允许,向上键将使您返回上一个键入的命令,从而加快生成模型的过程。
我们可以使用 OS-native shell 来做到这一点。我们必须编写 PowerShell 脚本来执行此任务。
这里是,
#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
}
.ps1
使用以 artisan 开头的扩展名(例如)保存文件并使用命令
artisan-models.ps1
直接运行 。这是链接.\artisan-models.ps1