• +91 9806724185,+91 9685733903
  • +91 9806724185

Migrations in Laravel 6 including the Schema builder comprised of the Schema and Blueprint facades.

Next, we'll continue building our CRM application by updating the users' table using migrations to add users with different roles like admin and user in our app.

What we check :

  • • How to check if a record/user exists in the database,

  • • How to create a migration for adding fields to existing database tables

CRM app will be hosted on the web and would be accessed by anyone who has the URL, we need to add some restrictions, so only users that belong to the owner company or organization can register for an account and log in.

There are certainly some advanced solutions for this kind of situations, but we can also use simple tricks such as:

Removing the registration link and route, and only allow admins to create accounts for the other users. Updating the login functionality and only allow users to log in after they are approved by admins upon registration.

Let's implement the first option. In this case, we need a way to create the first user which also needs to be the first admin in the application.

We can add a user manually to our database (In fact this will be the task of the final app users by directly creating a user in the database) and make it an admin but we can also implement this in the application by providing a first time registration form that allows a user to create an admin account and stoping showing it if there is at least one admin user in the database.

Now, how we can mark users as admins? We can do that by simply adding a role field in the users' table that takes either a user or admin values indicating,If the user is an admin or not. This simple role system is enough for us at this point of the tutorial but we'll see later how we add an advanced role and permissions system in our app.

After that, users can be created by the admin(s) using a form where we can supply the email and the role of the user. When saved, an email should be sent to users, so they can continue their registration by adding their name and password for their account.

We'll start by adding a role field to the users' table using a migration. But first, let's learn about migrations a little bit.

You can also solve this by providing a configuration interface for our application that gets started when the app is launched for the first time.

Steps for Laravel 6 Migrations :

Migrations are PHP files that contain a class with the andup()down() methods. The up() method is invoked when the migration is applied for adding changes to the database while the down() method is used to revert the applied changes.

Migrations are a powerful tool that enables developers to add changes to existing database tables such as adding or dropping fields without deleting the existing data. This is more important in production than development.

What you can do with Laravel migrations?

Laravel Migrations allow you to do a kind of version control for your database structure. This helps you easily deploy the next version of your application and revert to previous versions.

They also allow you to change the structure of your database tables by adding, renaming or dropping fields or add complete new tables without losing the existing data.

Laravel Migrations 101 :

Applied a set of builtin Laravel migrations for adding some essential tables for the working of the auth system like the users' table. Now, let's see how to create our migrations and apply them.

You can create and apply migrations using Artisan by running the artisan make:migration and artisan
migrate
commands. Your migration files can be then found in the database/migrations folder. Artisan adds the date and time of when the migration is created as a prefix to the file name.

Note: You need to have configured your database for migrations to work.

By convention, Laravel makes use of the middle name from the name of the migration file as the name of the table in the migration class. But the make:migration command can also be passed the -create and -table parameters for respectively specifying the name of the table in the Schema::create() or Schema::table() methods.

Now, let’s suppose that you made a design error and you want to revert the changes applied with the last "batch" of migrations. You can simply type:

$ php artisan migrate:rollback

You may rollback a specific number of migrations by using the step option with the rollback command. For example, the following command will rollback the lastest migration:

php artisan migrate:rollback --step=1

You can also drop your database and reset all migrations by typing:

$ php artisan migrate:reset

These are the three steps required to create and apply a migration to your database:

  • Step 1: Create a migration file using the php artisan make:migration command. For example, we can create a new users table in the database using php artisan make:migration
    create_users_table --create=users
    .
  • Step 2: Define or update the schema of your table by specifying the fields of the new table or add/drop new fields to/from an existing table.
  • Step 3: The final step is running the migration using the php artisan migrate command.

    Contact us for More Information

Related Posts

Technical Solutions

Steps to Improve Performance of Node Servers

  In the past, Node.js Development was often not an option when building applications that require CPU intensive computation. This is due to its non-blocking, event-driven I/O architecture. With the advent of thread workers in Read more…

Technical Solutions

Get Benefits with Angularjs and React Combination

  If you’re a JavaScript developer and know a little bit about the current state of JS development, then you’re probably well aware that there seems to be a new JS framework popping up every Read more…

Technical Solutions

Top 5 Risks of Utilizing Outdated Technology for Websites

Modern Websites and Apps need to be implemented on new technologies because of various reasons. In current days, lots of technologies are outdated which are not providing quality services in this competitive world. You have Read more…