Ad
How To Seed Specific Table As Part Of Migration - Laravel 5?
I have this commands
php artisan db:seed --class=GraphsTableSeeder
that I need to run in my part of my code (migration).
How do I do that ?
<?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()
{
//
}
}
I'm kind of stuck, please help.
Ad
Answer
Please check the 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()
{
//
}
}
Ad
source: stackoverflow.com
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
Ad