为自定义Laravel软件包提供数据的最佳方法是什么?

时间:2015-10-30 12:48:48

标签: php laravel

我正在为Laravel创建一个自定义包,需要从数据库中获取一些默认数据(大小格式,质量,价格范围)才能工作。

Laravel应用程序应该可以编辑数据(例如:价格变化),因此需要由程序包共享。因此,我为包需要使用的表创建了一些迁移,但是提供填充表的默认数据的最佳方法是什么?

2 个答案:

答案 0 :(得分:1)

将迁移中的默认值的种子代码放在我身上,例如:

<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateVocabulariesTable extends Migration
{

    public function up()
    {
        Schema::create('vocabularies', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 100)->nullable();

            $table->dateTime('created_at')->nullable();
            $table->dateTime('updated_at')->nullable();
            $table->softDeletes();
        });

        $records = [['id' => 1, 'name' => 'category'], ['id' => 2, 'name' => 'tag']];
        foreach ($records as $record)
            \App\Models\Vocabulary::create($record);
    }

    public function down()
    {
        if (Schema::hasTable('vocabularies')){
            Schema::drop('vocabularies');
        }
    }
}

答案 1 :(得分:0)

Laravel提供了在您使用迁移时使用的种子http://laravel.com/docs/5.1/seeding

相关问题