Populating a database in a Laravel migration file

I am just learning Laravel and have a working migration file that creates a user table. I'm trying to populate a user record as part of a migration:

public function up() { Schema::create('users', function($table){ $table->increments('id'); $table->string('email', 255); $table->string('password', 64); $table->boolean('verified'); $table->string('token', 255); $table->timestamps(); DB::table('users')->insert( array( 'email' => 'name@domain.com', 'verified' => true ) ); }); } 

But when you run php artisan migrate , the following error appears:

 SQLSTATE[42S02]: Base table or view not found: 1146 Table 'vantage.users' doesn't exist 

This is obviously because Artisan has not yet created the table, but all the documentation seems to suggest that there is a way to use Fluent Query to populate data as part of the migration.

Does anyone know how? Thank!

+94
php migration laravel mysql-error-1146
04 Oct
source share
7 answers

Do not put DB :: insert () inside the Schema :: create () schema, because the create method must finish creating the table before you can insert material. Try instead:

 public function up() { // Create the table Schema::create('users', function($table){ $table->increments('id'); $table->string('email', 255); $table->string('password', 64); $table->boolean('verified'); $table->string('token', 255); $table->timestamps(); }); // Insert some stuff DB::table('users')->insert( array( 'email' => 'name@domain.com', 'verified' => true ) ); } 
+192
04 Oct
source share

I know this is an old post, but since it appears in a Google search, I thought I would share some knowledge here. @ erin-geyer noted that mixing migrations and seeders can be a headache, and @justamartin contrasts with what you sometimes need / need data to be filled in as part of your deployment.

I would take one more step and say that sometimes it is desirable to be able to consistently deploy data changes so that you can, for example, deploy at the stage, see that everything is fine, and then deploy the same results with confidence (and do not need to remember to complete some manual step).

However, importance remains for seed separation and migration, as these are two related but different problems. Our team has compromised the creation of migrations called seeders. It looks like this:

 public function up() { Artisan::call( 'db:seed', [ '--class' => 'SomeSeeder', '--force' => true ] ); } 

This allows you to run the seed once, like a carry. You can also implement logic that prevents or enhances behavior. For example:

 public function up() { if ( SomeModel::count() < 10 ) { Artisan::call( 'db:seed', [ '--class' => 'SomeSeeder', '--force' => true ] ); } } 

This will obviously conditionally execute your seeder if there are less than 10 SomeModels. This is useful if you want to turn on the seeder as a standard seeder that runs when you call artisan db:seed , and also when migrating so that you don't double. You can also create a reverse seeder so that rollbacks work as expected, such as

 public function down() { Artisan::call( 'db:seed', [ '--class' => 'ReverseSomeSeeder', '--force' => true ] ); } 

The second parameter --force is required to start the seeder in the production environment.

+71
Sep 30 '15 at 2:43
source share

Here is a very good explanation of why using Laravel Database Seeder is preferable to using Migrations: http://laravelbook.com/laravel-database-seeding/

Although, following the instructions in the official documentation, this is much better, because the implementation described in the link above does not seem to work and is incomplete. http://laravel.com/docs/migrations#database-seeding

+11
May 22 '14 at 20:19
source share

This should do what you want.

 public function up() { DB::table('user')->insert(array('username'=>'dude', 'password'=>'z19pers!')); } 
+2
Nov 21 '12 at 5:11
source share

Another easy way to do this is to define a private method that instantiates and stores the model concerned.

 public function up() { Schema::create('roles', function (Blueprint $table) { $table->increments('id'); $table->string('label', 256); $table->timestamps(); $table->softDeletes(); }); $this->postCreate('admin', 'user'); } private function postCreate(string ...$roles) { foreach ($roles as $role) { $model = new Role(); $model->setAttribute('label', $role); $model->save(); } } 

With this solution, timestamp fields will be generated by Eloquent.

EDIT: It is best to use a seeder system to distinguish between the structure of the database and populate the database.

0
Dec 20 '18 at 16:03
source share

I tried this insert method in the database, but since it does not use the model, it ignored the flaccid trait that I had in the model. So, considering that the Model for this table exists as soon as it is ported, I decided that the model will be available for use to insert data. And I came up with this:

 public function up() { Schema::create('parent_categories', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name'); $table->string('slug'); $table->timestamps(); }); ParentCategory::create( [ 'id' => 1, 'name' => 'Occasions', ], ); } 

This worked correctly, and also took into account the character trait of my model to automatically generate a slug for this entry, and also uses timestamps. NB. Adding an identifier was not necessary, however in this example I wanted to specify specific identifiers for my categories. Tested running on Laravel 5.8

0
Aug 24 '19 at 10:24
source share

try: (not verified)

 public function up() { Schema::table('users', function($table){ $table->increments('id'); $table->string('email', 255); $table->string('password', 64); $table->boolean('verified'); $table->string('token', 255); $table->timestamps(); $table->insert( array( 'email' => 'name@domain.com', 'verified' => true ) ); }); } 
-13
04 Oct
source share



All Articles