1

I am creating a custom package for Laravel that need fetch some default data (size formats, qualities, price-ranges) from a database in order to work.

The data is supposed to be editable (ex.: the price changes) by the Laravel application so it needs to be shared by the package. I have therefore created some migrations for the tables that the package will need to use, but what is the best way to provide the default data that will populate the tables?

atlazor
  • 193
  • 8

2 Answers2

1

Put seeding code for default values in migrations works for me, something like:

<?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');
        }
    }
}
fico7489
  • 6,416
  • 5
  • 44
  • 77
  • Thanks. I was thinking of that but it seems kind of hacky (tbh. I don't know why i think so), but it may be the best solution. But do you have any methods or examples of how I may insert a larger amount of data? Should i provide an extra file containing all the data and then load and insert the data from said file in the migration code? – atlazor Oct 30 '15 at 13:09
  • It is matter of code organisation, so there is no perfect solution. – fico7489 Oct 30 '15 at 13:19
  • Ok. Thanks. I think I will use a combination of your answer and Carlos Mora's answer. I will use a migration that calls a seeder as shown in [this answer](http://stackoverflow.com/a/32856878/2083960) to a similar question. – atlazor Oct 30 '15 at 13:33
0

Laravel provides seeding http://laravel.com/docs/5.1/seeding to use as you use migrations.

Carlos Mora
  • 1,032
  • 1
  • 12
  • 26
  • Thank you. Of that I am aware, but do you have any methods or examples of how I may seed a larger amount of data? Two of my rows contains a large amount of information that is used in a complicated calculation. Is the best way to use seeders when populating a large amount of data? – atlazor Oct 30 '15 at 12:58
  • I think I will use a combination of your answer and fico7489's answer. I will use a migration that calls a seeder as shown in [this answer](http://stackoverflow.com/a/32856878/2083960) to a similar question. – atlazor Oct 30 '15 at 13:34
  • It depends on how your data looks like. Writing records inline may be boring, confusing and will produces ugly code. May be your data can be stored in other format (csv,json) in an external config file, part of app's configuration and then read it in your migration for seeding. – Carlos Mora Oct 30 '15 at 14:01
  • To keep the data separated from the code I have decided to read the data from a separate file (csv) in the seeder. Thanks for all the help! – atlazor Oct 30 '15 at 14:14