Setup Menus in Admin Panel

  • No products in the cart.

  • LOGIN

Migration concepts and Database setup

Migration


In Laravel, migrations provide mechanisms for creating and modifying database tables. Migrations are typically paired with Laravel’s schema builder to easily build your application’s database schema. If you have ever had to tell a teammate to manually add a column to their local database schema, you’ve faced the problem that database migrations solve. Migrations also allow you to roll back the most recent changes that you made to a database.

Let’s create a migration table step by step:

Step 1:

Open a project and go to database/migration and open the create_users_table.php
By default it has given the following code:

Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();

 

 

Fig:Screenshot showing the deafult database

 

Based on the requirement create a users table:
I have added some extra fields:

Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->rememberToken();
            $table->timestamps();
            $table->string('username',100);
            $table->string('email',100)->unique();
            $table->string('password',100);
            $table->string('full_name',100);
            $table->string('contact_no',50)->nullable();
            $table->text('address')->nullable();
            $table->boolean('status')->default(0);

 

Fig:Screenshot shows the added fields in the table.

 

Note:
If you make changes in your project don't forget to give the following command:
1.git add .
2.git commit -m "changes you made"
3.git push origin master
It means you have successfully made changes in your gitlab repository.

Screenshot:

Fig: Screenshot shows you have successfully push your project in gitlab.

 

Step 2:(Method 1 to create a table)

If you wish to create some other tables then you can simply create by using the following command:

php artisan make:migration create_category_table

Make sure you are in the current working directory i.e /c/xampp/htdocs/Laravel_Project/news_portal

Screenshot:

Fig:Created a table name category

You have successfully created your table:

Based on the requirement you can create your table.

Or,

(Method 2 to create a table)
Delete the table which you have created recently.
Remember if you have deleted the table which you have created by artisan command then you have to perform the following command:

composer dump-autoload

Screenshot:

 

Command to create a  table:

php artisan make:migration create_category_table --create=categories
php artisan make:migration create_news_table --create=news
php artisan make:migration create_about_table --create=about

 

Note:
up() function:
It helps to create a table.

down() function:
If we wish to rollback(changes) which is created recently then this function help to perform this task.

Vendor:
It is a library which is used during the coding time.

 

Project Setup:


Let’s go to the live project:

Step 1:
Download the template for news portal. You can get thousands of free templates.

Step 2:
Download the template for admin module.

Step 3:
Open the project i.e news_portal in your favorite editor. I am using the editor as PHPStorm.
Open the .env file and make the following changes:

By default:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
Changed:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=news_portal
DB_USERNAME=root
DB_PASSWORD=

 

SCREENSHOT 1:

 

 

Note:
1. We won’t upload the .env file in our live host project. So we have to make changes in the database.php i.e config/database.php directory.

            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge_np'),
            'password' => env('DB_PASSWORD', '1234'),

 

SCREENSHOT:

 

2.Go to the config/app.php directory and make the following changes:


By default:
'url' => env('APP_URL', 'http://localhost'),
Change:
'url' => env('APP_URL', 'http://yourdomain.com'),

 

SCREENSHOT:

 

Remember:
.env file is for the development purpose that is only used during the coding time.

 

Now migrate all the table by typing the following command:

php artisan migrate

 

Screenshot:

 

Screenshot of database

 

Note:

If you made some changes in your code then you have to perform the following command:

php artisan migrate:rollback

 

 

The above rollback command removes all the table in your database except migrations.

 

Screenshot :
code changes

 

Now drop the migrations table from the localhost as in the screenshot below:

Screenshot:

 

 

Then again perform the following command:

php artisan migrate

Screenshot:

Screenshot:
database table

 

Suppose if you wish to add some table then you can add by using the following command:

php artisan make:migration create_site_table --create=siteprofiles

Now it is not a necessity to drop all the database table. Simply you can perform the following command which helps to change only the last created table.

php artisan migrate

Screenshot:

 

Screenshot:
database table

Note:
The newly created will be the batch 2.

 

Screenshot:

 

Batch uses:
Suppose if you want to rollback then first it will remove the latest data i.e batch 2.
Again if you want to rollback then it will remove the tables which are in batch 1.

refresh command:
This command is used to rollback all migrations and run them all again.

php artisan migrate:refresh

Screenshot:

Note:
If you made some changes then this command is the shortest command to perform rollback and migration at a single time.

 

 

Columns


Creating Columns:

The table method on the Schema facade may be used to update existing tables. Like the create method, the table method accepts two arguments: the name of the table and a Closure that receives a Blueprint instance you may use to add columns to the table:

Schema::table('users', function (Blueprint $table) {
$table->string('email');
});

 

Screenshot 1:

 

Screenshot 2:



Note:
Here, table is used to alter the data.

 

Command:

php artisan make:migration add_gender_to_user_table --table=users

Then run artisan command:

php artisan migrate

 

Screenshot:

 

Note:
This command will only alter the latest data which we have modified.

Screenshot:

 

Here you have seen gender data is created at the end of table. If you need to arrange the data then you have to perform the following command:

php artisan migrate:rollback

Then modify in the code:

Code:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddGenderToUserTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('gender')->default('male')->after('full_name');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('gender');
});
}
}

 

 

Screenshot:

 

Finally, perform the following command:

php artisan migrate

Screenshot:

 

Attachments26

SEE ALL Add a note
YOU
Add your Comment
 

Advanced Course Search Widget

Popular Courses

Course Categories

© PHPhelp. 2002 - 2018 All rights reserved.
X