laravel migrate 数据库 新建表
新建表migration
php artisan make:migration create_order_pinks_table --create=order_pinks
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateOrderPinksTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('order_pinks', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('order_id'); $table->string('name_cn'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('c_order_pinks'); } }
查看migration 状态
php artisan migrate:status
执行migration
php artisan migrate
撤销上一次执行的migrate 回滚
php artisan migrate:rollback
撤销回滚最近五次的migrate迁移
php artisan migrate:rollback --step=5
回滚所有迁移并重新构建整个数据库
php artisan migrate:refresh
撤销回滚程序中所有迁移
php artisan migrate:reset
回滚并重新执行最后五次迁移
php artisan migrate:refresh --step=5
删除数据库中所有表,并执行migrate
php artisan migrate:fresh php artisan migrate:fresh --seed
seeder 填充数据
创建seeder,编写填充数据
php artisan make:seeder OrderPinkTableSeeder
<?php use Illuminate\Database\Seeder; use App\Models\Prod\OrderPink; use Illuminate\Support\Facades\DB; class OrderPinkTableSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { // DB::statement('SET FOREIGN_KEY_CHECKS = 0'); DB::statement('SET FOREIGN_KEY_CHECKS = 1'); OrderPink::truncate(); $datas = [ ['order_id'=>1, 'name_cn'=>'乙醇'], ['order_id'=>2, 'name_cn'=>'甲烷'], ]; OrderPink::insert($datas); } }
执行填充数据
php artisan db:seed --class=OrderPinkTableSeeder
删除migration
一、如果只新建了migration,发现名称输错了,还没有执行php artisan migrate,可以执行以下命令
-
删除migration目录下的migration文件
-
重置composer文件加载项: composer dump-autoload
-
重新创建新的migrations文件了
二、如果新建了migration,同时也执行了php artisan migrate,可以执行以下命令
1.php artisan migrate:rollback 运行这个命令正确的回滚上一次进行的所有migrate操作
2.如果运行命令 migrate:rollback 失败了,那么,请按照如下方式操作
2.1 删除migrations目录下错误的migrations文件,比如删除 app/database/migrations/my_migration_file_name.php
2.2 重置composer文件加载项: composer dump-autoload
2.3 删除数据库里的表,重新生成migrations文件进行操作