drvjlec1767 2018-07-03 15:57
浏览 189
已采纳

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');
    }
}

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.

enter image description here

  • 写回答

5条回答 默认 最新

  • drh96824 2018-07-05 21:34
    关注

    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.

    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).

    Example:

    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"
    
        ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?