Database Migrations In Laravel 2018- Votive Technologies |

Laravel – Database Migrations 2018

Laravel – Database Migrations 2018

migration-in-laravel

Laravel offers an easy to use and influential way to create database structure using code. This method allows you to define your database tables and columns in text files, which makes it easy to edit and develop as needed. Laravel also provides a built-in-rollback technique that allows a user to revert back any database structure changes as required. We are sharing useful knowledge about Laravel database migrations and how to use them.

Project:

We are giving example for restaurant review portal, users will be able to submit restaurant reviews using a PHP form, and view existing restaurant reviews that stored in a MySQL database.

Requirement:
A 1&1 Cloud Server with Laravel and PHP 7.0+ installed.
Create the Database:
Firstly, you require creating a database for your project, and a database user for Laravel to use. Log in to the MySQL client:
MySQL -u root –p

Create a database of the reviews:
CREATE DATABASE reviews_db;
The subsequent command will create a user review_site with password JxSLRkdutW and allow the user to access the reviews_db database:
GRANT ALL ON reviews_db.* to review_site@localhost IDENTIFIED BY ‘JxSLRkdutW’;

Create the Laravel Project:
Use su to switch to your Laravel user:
su – jdoe

Go to your web directory:
cd /var/www/html
Create the Laravel project:
laravel new reviews
It will create the reviews directory. Move into this directory:
cd reviews
Now, set the MySQL credentials in the Laravel environment file. Open the .env file for editing:
nano .env
Find the section which reads:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
Change the DB_DATABASE, DB_USERNAME, and DB_PASSWORD lines to contain your project’s MySQL database name, username, and password:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=reviews_db
DB_USERNAME=review_site
DB_PASSWORD=JxSLRkdutW
Save and exit the file.
Create and Run the Migration
Laravel migrations work as a version control system for your database. It will allow a user to easily collaborate and edit database design.
Use the make:migration Artisan command to create a migration named create_reviews_table. We will also include the –create flag to create the table user_reviews:
php artisan make:migration create_reviews_table –create=user_reviews
The new migration will be situated in the database/migrations directory, and will have a name which includes the timestamp:

jdoe@localhost:/var/www/html/reviews$ ll database/migrations
total 20
drwxrwxr-x 2 jdoe jdoe 4096 Oct 26 16:16 ./
drwxrwxr-x 5 jdoe jdoe 4096 Oct 26 16:15 ../
-rw-rw-r– 1 jdoe jdoe 746 Oct 26 16:15 2014_10_12_000000_create_users_table.php
-rw-rw-r– 1 jdoe jdoe 683 Oct 26 16:15 2014_10_12_100000_create_password_resets_table.php
-rw-rw-r– 1 jdoe jdoe 602 Oct 26 16:16 2017_10_26_161616_create_reviews_table.php
All migration class includes two methods: up() and down(). The up() method will add new architecture (tables, columns, indexes, etc.) to the database. The down() method will reverse operations which have been performed by the up() method.

Open the database migration file for editing:
nano database/migrations/2017_10_26_161616_create_reviews_table.php
All migration automatically includes the auto-incrementing id column and the timestamps() columns. So we are adding three columns to the table:
reviewer_name: A string with a maximum of 100 characters.
star_rating: An integer.
details: A text field.
For a full list of column types, check the official Laravel documentation on migrations.
Locate the up() method section in the migration file:
public function up()
{
Schema::create(‘user_reviews’, function (Blueprint $table) {
$table->increments(‘id’);
$table->timestamps();
});
}
Add the new column definitions so that this section reads:
public function up()
{
Schema::create(‘user_reviews’, function (Blueprint $table) {
$table->increments(‘id’);
$table->timestamps();
$table->string(‘reviewer_name’, 100);
$table->integer(‘star_rating’);
$table->text(‘details’);
});
}
Save and exit the file. Then run the migration:
php artisan migrate
If you need to revert (roll back) a migration, use the command:
php artisan migrate:rollback

Hope knowing Database Migrations In Laravell it would help you when you develop a laravell website.If you are looking for any help to develop a website just contact us to connect with our E commerce website expert developer.

Summary
Article Name
Database Migrations In Laravel 2018
OurAchievements
Mobile Experience

3+ Years
Mobile Experience

Web Experience

6+ Years
Web Experience

online Marketing

5+ Years
online Marketing

Worldwide Clients

700+
Worldwide Clients

Shares