laravel从模型生成数据库 [英] laravel generate database from model

查看:88
本文介绍了laravel从模型生成数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel的现有项目,并且该现有项目已经具有模型,这里是一个示例:

I am using an existing project of Laravel and this existing project already has models, here is an example of one:

<?php

/**
 * Created by Reliese Model.
 * Date: Fri, 20 Apr 2018 08:56:36 +0000.
 */

namespace App\Models;

use Reliese\Database\Eloquent\Model as Eloquent;

/**
 * Class PdTcountry
 * 
 * @property int $pkcountry
 * @property string $country_code
 * @property string $country_name
 * @property string $country_localName
 * @property string $country_webCode
 * @property string $country_region
 * @property string $country_continent
 * @property float $country_latitude
 * @property float $country_longitude
 * @property string $country_surfaceArea
 * @property string $country_population
 * @property string $country_postcodeexpression
 * @property \Carbon\Carbon $create_at
 * @property \Carbon\Carbon $update_at
 * 
 * @property \Illuminate\Database\Eloquent\Collection $pd_tregions
 *
 * @package App\Models
 */
class PdTcountry extends Eloquent
{
    protected $table = 'pd_tcountry';
    protected $primaryKey = 'pkcountry';
    public $timestamps = false;

    protected $casts = [
        'country_latitude' => 'float',
        'country_longitude' => 'float'
    ];

    protected $dates = [
        'create_at',
        'update_at'
    ];

    protected $fillable = [
        'country_code',
        'country_name',
        'country_localName',
        'country_webCode',
        'country_region',
        'country_continent',
        'country_latitude',
        'country_longitude',
        'country_surfaceArea',
        'country_population',
        'country_postcodeexpression',
        'create_at',
        'update_at'
    ];

    public function pd_tregions()
    {
        return $this->hasMany(\App\Models\PdTregion::class, 'fkcountry');
    }
}

我的问题是,有了这个Model,是否可以通过php artisan从该模型创建数据库表?如果有一个php artisan命令可以对我所有的模型进行超级处理.

My question is, with this Model is there away via php artisan to create a database table from the model? If there is a php artisan command to do it for all my models that would be super.

在我的数据库文件夹中,有这些,但我不知道它们的作用.

In my database folder I have these, but I don't know what they do.

推荐答案

如果您希望自动生成这些表,那么否,Laravel确实没有做到这一点的方法.从理论上讲,您可以编写自己的命令为每个模型生成迁移文件,但是无论如何仍然需要您提供所有列名,数据类型等.例如,看一下Laravel的make:migration命令.它仅使用存根文件,并在生成(vendor/laravel/framework/src/Illuminate/Database/Migrations/MigrationCreator.php)时替换关键字.如果您有大量需要数据库表的模型,那么这可能是您学习的好方法.

If you are looking to generate these tables automagically, then no, Laravel doesn't really have a way to do that. In theory, you could write your own command to generate migration files for each of your models, but it will still require you to provide all the column names, data types, etc. anyways. Take a look at Laravel's make:migration command for instance. It just uses stub files and replaces key words when generating (vendor/laravel/framework/src/Illuminate/Database/Migrations/MigrationCreator.php). If you have a ton of models that need database tables, then maybe this would be a good approach for you to dive into.

但是,如果不是这样,最好使用标准命令生成迁移,并为其提供--create标记.之后,您只需要在模型中定义表(或使用命名约定,以便它可以自动找到它,请参见: https://laravel.com/docs/5.6/eloquent#defining-models 了解有关命名约定的更多信息.

If not, however, you're probably best off generating a migration using the standard command and just supply it with the --create tag. Afterwards, you would just have to define your table in your model (or use the naming convention so it finds it automatically, see: https://laravel.com/docs/5.6/eloquent#defining-models for more info on the naming conventions).

示例:

php artisan make:migration create_my_model_table --create=my_model_table_name

如果您不使用命名约定,则将表名添加到模型中:

If you don't use the naming convention, add your table name to your model:

class PdTcountry extends Eloquent {

    protected $table = "my_model_table_name"

    ...
}

这篇关于laravel从模型生成数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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