如何作为迁移的一部分播种特定 table - Laravel 5?
How to seed specific table as part of migration - 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()
{
//
}
}
我有点卡住了,请帮忙。
请检查 link Programmatically Executing Commands
<?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()
{
//
}
}
我有这个命令
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()
{
//
}
}
我有点卡住了,请帮忙。
请检查 link Programmatically Executing Commands
<?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()
{
//
}
}