使用laravel 的artisan快速创建表、模型、控制器、批量填充数据

  • 创建migrate文件
 php artisan make:migration create_comments_table
  • 编辑migrate文件
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateProductsTable extends Migration
{
    /**
     * Run the migrations.
     * up方法用于创建数据表
     * @return void
     */
    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name')->nullable();//表示字段可以为空
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     * down方法用于回滚也就是删除数据表
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('products');
    }
}
命令描述
$table->bigIncrements(‘id’);递增 ID(主键),相当于「UNSIGNED BIG INTEGER」
$table->bigInteger(‘votes’);相当于 BIGINT
$table->binary(‘data’);相当于 BLOB
$table->boolean(‘confirmed’);相当于 BOOLEAN
$table->char(‘name’, 4);相当于带有长度的 CHAR
$table->date(‘created_at’);相当于 DATE
$table->dateTime(‘created_at’);相当于 DATETIME
$table->dateTimeTz(‘created_at’);相当于带时区 DATETIME
$table->decimal(‘amount’, 8, 2);相当于带有精度与基数 DECIMAL
$table->double(‘column’, 8, 2);相当于带有精度与基数 DOUBLE
$table->enum(‘level’, [‘easy’, ‘hard’]);相当于 ENUM
$table->float(‘amount’, 8, 2);相当于带有精度与基数 FLOAT
$table->geometry(‘positions’);相当于 GEOMETRY
$table->geometryCollection(‘positions’);相当于 GEOMETRYCOLLECTION
$table->increments(‘id’);递增的 ID (主键),相当于「UNSIGNED INTEGER」
$table->integer(‘votes’);相当于 INTEGER
$table->ipAddress(‘visitor’);相当于 IP 地址
$table->json(‘options’);相当于 JSON
$table->jsonb(‘options’);相当于 JSONB
$table->lineString(‘positions’);相当于 LINESTRING
$table->longText(‘description’);相当于 LONGTEXT
$table->macAddress(‘device’);相当于 MAC 地址
$table->mediumIncrements(‘id’);递增 ID (主键) ,相当于「UNSIGNED MEDIUM INTEGER」
$table->mediumInteger(‘votes’);相当于 MEDIUMINT
$table->mediumText(‘description’);相当于 MEDIUMTEXT
$table->morphs(‘taggable’);相当于加入递增的 taggable_id 与字符串 taggable_type
$table->multiLineString(‘positions’);相当于 MULTILINESTRING
$table->multiPoint(‘positions’);相当于 MULTIPOINT
$table->multiPolygon(‘positions’);相当于 MULTIPOLYGON
$table->nullableMorphs(‘taggable’);相当于可空版本的 morphs() 字段
$table->nullableTimestamps();相当于可空版本的 timestamps() 字段
$table->point(‘position’);相当于 POINT
$table->polygon(‘positions’);相当于 POLYGON
$table->rememberToken();相当于可空版本的 VARCHAR(100) 的 remember_token 字段
$table->smallIncrements(‘id’);递增 ID (主键) ,相当于「UNSIGNED SMALL INTEGER」
$table->smallInteger(‘votes’);相当于 SMALLINT
$table->softDeletes();相当于为软删除添加一个可空的 deleted_at 字段
$table->softDeletesTz();相当于为软删除添加一个可空的 带时区的 deleted_at 字段
$table->string(‘name’, 100);相当于带长度的 VARCHAR
$table->text(‘description’);相当于 TEXT
$table->time(‘sunrise’);相当于 TIME
$table->timeTz(‘sunrise’);相当于带时区的 TIME
$table->timestamp(‘added_on’);相当于 TIMESTAMP
$table->timestampTz(‘added_on’);相当于带时区的 TIMESTAMP
$table->tinyIncrements(‘id’);相当于自动递增 UNSIGNED TINYINT
$table->tinyInteger(‘votes’);相当于 TINYINT
$table->unsignedBigInteger(‘votes’);相当于 Unsigned BIGINT
$table->unsignedDecimal(‘amount’, 8, 2);相当于带有精度和基数的 UNSIGNED DECIMAL
$table->unsignedInteger(‘votes’);相当于 Unsigned INT
$table->unsignedMediumInteger(‘votes’);相当于 Unsigned MEDIUMINT
$table->unsignedSmallInteger(‘votes’);相当于 Unsigned SMALLINT
$table->unsignedTinyInteger(‘votes’);相当于 Unsigned TINYINT
$table->uuid(‘id’);相当于 UUID
$table->year(‘birth_year’);相当于 YEAR
  • 创建表
php artisan migrate

若提示1071 Specified key was too long; max key length is 1000 bytes
则是因为Laravel 5.4改用4字节长度的utf8mb4,而只有MySql 5.5.3版本以后才开始支持utf8mb4字符编码。版本不一致导致。
解决方案
1)升级MySql版本到5.5.3以上。

2)在AppServiceProvider的 boot 方法里设置默认字符串的长度

    public function boot()
    {
        Schema::defaultStringLength(191);
    }

laravel-admin中模型及控制器的创建使用步骤

  • 创建model
php artisan make:model Product
  • 创建控制器
php artisan admin:make ProductController --model=App\\Product

// 在windows系统中
php artisan admin:make ProductController --model=App\Product

上面的命令会创建路由器文件app/Admin/Controllers/ProductController.php

  • 添加路由配置

在laravel-admin的路由配置文件app/Admin/routes.php里添加一行:

$router->resource('products', ProductController::class);
  • 添加左侧菜单栏连接
    打开http://localhost:8000/admin/auth/menu,添加对应的menu

然后就能在后台管理页面的左侧边栏看到用户管理页面的链接入口了。

  • 批量填充数据
    第一步:/database/factories/ 目录下有个ModelFactory.php 文件添加代码
$factory->define(\App\Product::class, function (\Faker\Generator $faker) {
    return[
        'name' => $faker->sentence(6),
    ];
});

第二步:在shell 里 用命令 php artisan tinker

第三步:factory(App\Post::class,20)->create(); 这个语句是直接写到数据库里的

而factory(App\Post::class,20)->make(); 是打印在页面上的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Emma'

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值