Lumen(Laravel)中的Eloquent,php artisan make:model未定义。

47

我在一个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

Laravel Eloquent文档 (http://laravel.com/docs/5.1/eloquent#defining-models)。

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

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

添加细节

php artisan --version

显示:

Laravel Framework version Lumen (5.1.6) (Laravel Components 5.1.*)
6个回答

47

你看到这个错误是因为Lumen没有make:model命令。

如果想查看可用的artisan命令列表,请运行php artisan

话虽如此,我刚刚找到了这个包,并将其添加到一个Lumen安装中,它似乎可以正常工作。https://github.com/webNeat/lumen-generators#installation

希望这可以帮到你!


37

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

只需使用composer安装即可:

composer require flipbox/lumen-generator

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

$app->register(Flipbox\LumenGenerator\LumenGeneratorServiceProvider::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


14
  1. 前往项目目录,使用以下命令将生成器包添加到您的composer.json

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

  • public function register()
    {
        if ($this->app->environment() == 'local') {
            $this->app->register('Wn\Generators\CommandsServiceProvider');
        }
    }
    
    请确保您取消注释了bootstrap/app.php文件中的以下行,以允许在项目中使用服务提供者:
    $app->register(App\Providers\AppServiceProvider::class);
    
  • 在项目目录(文档根目录)中运行php artisan list。现在你会看到那里有新的条目。


  • 2

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

    示例

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

    在Lumen中应该使用 Jenssegers\Mongodb\Eloquent\Model 还是 Illuminate\Database.. - Nandha

    0

    请更明显地展示此回答的额外贡献,例如比 johannchopin 更古老且已经得到赞同的回答。 - Yunnosch

    0

    将$app->register(Flipbox\LumenGenerator\LumenGeneratorServiceProvider::class);这一行添加到"bootstrap\app.php"文件中并保存,然后执行命令即可。


    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接