Hi, if you are beginner and want to create database table in laravel 8 so this post is very useful. in laravel we need to create database table through migration. laravel providing migration command that helps we can create/remove database table and its column.
Table of contents
1) Migration
2) Rollback
3) add new column in already created table
in this example, I will create advertisements table through migration.
1) Migration
you can easily create migration for manage database table. here I will create migration for advertisements section.
php artisan make:migration create_advertisements_table
after run command it will create migration file under database\migrations called 2023_01_28_183023_create_advertisements_table.php . now we need to open that migration file and add column where I want to put in advertisements table.
2023_01_28_183023_create_advertisements_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateAdvertisementsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('advertisements', function (Blueprint $table) {
$table->id();
$table->string('page_name')->nullable();
$table->enum('section',['top','middle','bottom','sidebar_panel'])->nullable();
$table->string('title')->nullable();
$table->text('discription')->nullable();
$table->date('start_date')->nullable();
$table->date('end_date')->nullable();
$table->tinyInteger('is_active')->default(1);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('advertisements');
}
}
I added some code to advertisements migration to create some columns like page_name, section, title, discription, start_date, end_date and is_active under advertisements table. we need to run migrate command to generate table.
php artisan migrate
after that, we can advertisements table have been generated.
2) Rollback
if you want to remove generated table so laravel providing rollback functionality. I am explaining you how to rollback last created migration table through command.
php artisan migrate:rollback --step=1
after run rollback command, if its executed successfully so we can see advertisements table have been removed/deleted.
3) add new column in already created table
if you want to add new column in already created table so we can do through laravel migration. in below example I will show how to add new column in table through migration.
we already have advertisements table in database.
here first fore, I need to create new migration for generate new column in database table in laravel. I will create migration called add_fields_advertisements_table.
php artisan make:migration add_fields_advertisements_table
after run command, we need open generated add_fields_advertisements_table migration.
2023_01_28_190938_add_fields_advertisements_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddFieldsAdvertisementsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
//
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
now we need add columns fields name here so want to add in database table.
in advertisements table, I will add order field/column in advertisements table. where I can set order number like 1,2,3 etc from table.
now updated code in 2023_01_28_190938_add_fields_advertisements_table.php
2023_01_28_190938_add_fields_advertisements_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddFieldsAdvertisementsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('advertisements', function (Blueprint $table) {
$table->unsignedBigInteger('order')->nullable()->after('is_active');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('advertisements', function (Blueprint $table) {
$table->dropColumn('order');
});
}
}
we need to run migration command.
php artisan migrate
now i can see new column “order” have been added in database advertisements table . when we run php artisan migrate command so migration files “Up” code will execute. if we run rollback so it will execute “down” section code.
if you want to remove that added fields “order” from advertisements table so I added some code under “down” section. we can remove that column through roll back command.
php artisan migrate:rollback --step=1
after run command , I will check advertisements table “order” have been removed.
I have created many blog so you can take online education with Laravel section.
Hello, Welcome to webdeveloperindia.in. I am a full-stack web developer. Email – [email protected] | Skype – azaruddin23. I have knowledge of PHP, Laravel, Magento 1/2, Codeigniter, WordPress, Joomla, Shopify, Git, Bitbuket, jQuery, Ajax, Javascript and ReactJS.
Nice post, really get help to fixed my issue