Как заполнить таблицу c как часть миграции - Laravel 5? - PullRequest
0 голосов
/ 02 марта 2020

У меня есть эти команды

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()
    {
        //
    }
}

Я застрял, пожалуйста, помогите.

1 Ответ

1 голос
/ 02 марта 2020

Пожалуйста, проверьте ссылку Программно выполняемые команды

<?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()
    {
        //
    }
}
...