Lumen(Laravel)Eloquent php手工制作:模型未定义



我在API项目中使用Lumen 1.0。

我已经通过取消bootstrap/app.php文件中的以下行来启用Eloquent:

$app->withEloquent();

但当我想创建我的第一个迁移模型时,它失败了:

php artisan make:model Book --migration

错误消息:

  [InvalidArgumentException]
  Command "make:model" is not defined.
  Did you mean one of these?
      make:seeder
      make:migration

关于Eloquent的Laravel文档(http://laravel.com/docs/5.1/eloquent#defining-模型)。

Lumen文档(http://lumen.laravel.com/docs/installation)不包括Eloquent doc,因为默认情况下它没有启用。

你有什么想法可以避免这个错误吗?

添加详细信息

php artisan --version

显示器:

Laravel Framework version Lumen (5.1.6) (Laravel Components 5.1.*)

您看到这个错误是因为Lumen没有附带make:model

要查看您可以使用的所有手工命令的列表,只需运行php artisan

话虽如此,我确实刚刚找到了这个包,我把它添加到了管腔安装中,看起来效果很好https://github.com/webNeat/lumen-generators#installation

希望这能有所帮助!

如果您使用php artisan list检查所有可用的命令,您会发现您没有laravel提供的所有默认命令。但使用lumen-generator包(不要与lumen-generators混淆)可以获得最重要的内容。它的优点是提供了比其他提到的命令更多的命令。

要使用它,只需使用composer:安装即可

composer require flipbox/lumen-generator

然后在bootstrap/app.php文件中启用它:

$app->register(FlipboxLumenGeneratorLumenGeneratorServiceProvider::class);

您现在可以使用artisan:使用所有这些新命令

key:generate      Set the application key
make:command      Create a new Artisan command
make:controller   Create a new controller class
make:event        Create a new event class
make:job          Create a new job class
make:listener     Create a new event listener class
make:mail         Create a new email class
make:middleware   Create a new middleware class
make:migration    Create a new migration file
make:model        Create a new Eloquent model class
make:policy       Create a new policy class
make:provider     Create a new service provider class
make:seeder       Create a new seeder class
make:test         Create a new test class

只需查看官方文件:https://github.com/flipboxstudio/lumen-generator

  1. 转到项目目录,使用以下命令将generators包添加到composer.json中:

    composer require wn/lumen-generators
    
  2. 将以下代码段添加到app/Providers/AppServiceProvider.php:

    public function register()
    {
        if ($this->app->environment() == 'local') {
            $this->app->register('WnGeneratorsCommandsServiceProvider');
        }
    }
    
  3. 确保您在bootstrap/app.php中取消了对以下行的评论,以允许服务提供商参与您的项目:

    $app->register(AppProvidersAppServiceProvider::class);
    
  4. 在项目目录(文档根目录)上运行php artisan list。现在,您将在那里看到新项目。

只需在应用程序目录中手动创建模型文件

示例

<?php
namespace App;
use IlluminateDatabaseEloquentModel;
class Article extends Model {
    protected $table = ‘articles’;
    protected $fillable = [
        ‘title’,
        ‘description’,
        ‘body’
    ];
}

$app->register(Flipbox\LumenGenerator\LumenGeneratorServiceProvider::class);把这行加到";bootstrap\app.php";并保存此文件,然后发出命令。它会起作用的。

有一些包可以帮助您拥有Laravel上的所有artisan命令。安装以下软件包以获得更多artisan命令。https://github.com/flipboxstudio/lumen-generator

最新更新