php - 如何在迁移过程中播种特定表 - Laravel 5?
问题描述
我有这个命令
php artisan db:seed --class=GraphsTableSeeder
我需要在我的部分代码中运行(迁移)。
我怎么做 ?
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use App\Models\Graph;
class RescueGraphsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
if (Schema::hasTable('graphs')) {
$graphRecords = Graph::all();
if(count($graphRecords) == 42){
echo "graphs table has good data.";
}
//truncate
DB::table('graphs')->truncate();
//add data
//php artisan db:seed --class=GraphsTableSeeder ✨
} else {
//add graphs table
//add data
//php artisan db:seed --class=GraphsTableSeeder ✨
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
我有点卡住了,请帮忙。
解决方案
请检查链接以编程方式执行命令
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use App\Models\Graph;
class RescueGraphsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
if (Schema::hasTable('graphs')) {
if(Graph::count() == 42){
echo "graphs table has good data.";
}
DB::table('graphs')->truncate();
}
$exitCode = \Artisan::call('db:seed', [
'--class' => 'GraphsTableSeeder'
]);
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
推荐阅读
- html - 如何使用 RSS 源为 Podcast 创建播放器?
- java - 如何仅获取用户在 numGames() 中输入的值并避免程序打印“你叫什么名字?” 再次?
- operating-system - 在没有硬件支持的情况下实现抢占式调度的实用方法?
- java - 尝试编写计算句子中单词平均长度的代码
- parameters - 如何在 squarespace 中将 UTM 参数从一页传递到另一页?
- django - 如何使用 group by 执行 django 查询,以及将两列相乘?
- python - 用漂亮的汤抓取多个文件,每个文件作为列名
- c++ - 最终构造函数后自动调用方法
- django - 如何解决错误“RelatedManager”对象不可下标?
- laravel-8 - 显示错误后应保留表单数据