【问题标题】:How to seed specific table as part of migration - Laravel 5?如何在迁移过程中播种特定表 - Laravel 5?
【发布时间】:2020-06-14 22:29:21
【问题描述】:

我有这个命令

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 laravel laravel-5 database-migration


    【解决方案1】:

    请查看链接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()
        {
            //
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-10-25
      • 2016-06-19
      • 2016-06-09
      • 2016-05-09
      • 2020-07-07
      • 2014-08-14
      • 2019-04-09
      • 2016-09-11
      • 2017-07-10
      相关资源
      最近更新 更多