Insert values during migration run laravel
With migrations in laravel, we can create, modify database tables without using phpmyadmin. In Laravel migrations are stores in project_folder/database/migrations in proper order. Every migration file is associated with the table. We can create migration using php artisan command and the name of migration is combination of time and date when migration command executed (i.e. 2020_06_09_000000_create_users_table). We can create a table with its columns using this file. We can also add columns in any already created table. Migration structure consists of 2 functions: up and down. Up function is used to add, insert, modify table or columns and Down function is used to delete columns from a table or drop a table.
If you want to insert data during migration run in laravel then you can use seeders in Laravel, but I'm going to insert data using the same migration file. I will use DB in my migration file to insert data while run migration. In this migration it is important to create a table first and then insert the data into the created table. I'm using below code to insert data during laravel migration.
First I'm running the command to create migration
php artisan make:migration create_products_table
Modify products migration
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;class CreateProductsTable extends Migration
{
public function up()
{
Schema::create(`products`, function (Blueprint $table) {
$table->bigIncrements(`id`);
$table->string(`name`);
$table->float(`price`);
$table->timestamps();
});
DB::table(`products`)
->insert(array(
array(`name` => `Plan 1`,`price` => 10),
array(`name` => `Plan 2`,`price` => 50),
array(`name` => `Plan 3`,`price` => 100),
));
}
public function down()
{
Schema::dropIfExists('products');
}
}
After that Run Command
php artisan migrate
With migrations in laravel, we can create, modify database tables without using phpmyadmin. In Laravel migrations are stores in project_folder/database/migrations in proper order. Every migration file is associated with the table. We can create migration using php artisan command and the name of migration is combination of time and date when migration command executed (i.e. 2020_06_09_000000_create_users_table). We can create a table with its columns using this file. We can also add columns in any already created table. Migration structure consists of 2 functions: up and down. Up function is used to add, insert, modify table or columns and Down function is used to delete columns from a table or drop a table.
If you want to insert data during migration run in laravel then you can use seeders in Laravel, but I'm going to insert data using the same migration file. I will use DB in my migration file to insert data while run migration. In this migration it is important to create a table first and then insert the data into the created table. I'm using below code to insert data during laravel migration.
First I'm running the command to create migration
php artisan make:migration create_products_table
Modify products migration
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;class CreateProductsTable extends Migration
{
public function up()
{
Schema::create(`products`, function (Blueprint $table) {
$table->bigIncrements(`id`);
$table->string(`name`);
$table->float(`price`);
$table->timestamps();
});
DB::table(`products`)
->insert(array(
array(`name` => `Plan 1`,`price` => 10),
array(`name` => `Plan 2`,`price` => 50),
array(`name` => `Plan 3`,`price` => 100),
));
}
public function down()
{
Schema::dropIfExists('products');
}
}
After that Run Command
php artisan migrate
Recommanded Articles
- How to create a multilevel category and subcategory in Laravel
- How to check YouTube video exist Laravel validation
- Multiple user roles authentication Laravel 8
- Deploy Laravel project from local to production server
- Make custom pagination URL in Laravel without query strings
- Web Scraping in Laravel using Goutte
- Insert values during migration run laravel
- Validation for string characters only with custom message in Laravel
- Add new columns in a table Laravel
- How to create foreign key constraints in Laravel
Latest Comments
Nasir
21 Dec 2020