laravel使用php artisan时从自定义存根创建模型 [英] laravel create model from custom stub when using php artisan

查看:92
本文介绍了laravel使用php artisan时从自定义存根创建模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用php artisan make:model CustomNamespace\TestModel时,我会得到一个基于默认存根的模型,如下所示:

When I use php artisan make:model CustomNamespace\TestModel, I get a model based on default stub as this :

namespace App\Models\CustomNamespace;
use Illuminate\Database\Eloquent\Model;
class TestModel extends Model
{
    //
}

但是我要创建的是一个基于我自己的存根的动态模型,以获得类似这样的信息:

But what I want to create is a dynamic Model based on my own stub to get something like this:

namespace App\Models\CustomNamespace;

use App\Models\MyParent;
/**
 * Put a dynamic doc here
 */
class MyModel extends MyParent
{
    /*put custom methods here*/
}

我已经检查了Laravel文档和其他tuto,但是对此一无所获,您能帮帮我吗?

I've checked Laravel docs and other tutos but nothing on this, could you help guys ?

推荐答案

创建一个新命令,扩展Illuminate\Foundation\Console\ModelMakeCommand类并覆盖getStub()方法:

Create a new command, extend the Illuminate\Foundation\Console\ModelMakeCommand class and override the getStub() method:

protected function getStub()
{
    if ($this->option('pivot')) {
        return __DIR__.'/stubs/pivot.model.stub';
    }

    return storage_path('/stubs/my-own-model.stub');
}

这篇关于laravel使用php artisan时从自定义存根创建模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆