Adding Column in Existing Table Laravel (migration command code)

We can easily add remove existing column in existing table of our project. It is necessary to add column in exiting production database. Here i have share the steps to add column in existing table.

 

To create a migration, you may use the migrate:make command on the Artisan CLI. Use a specific name to avoid clashing with existing models

for Laravel 3:



php artisan migrate:make add_sankar_to_tbl_admin_menu1

 

for Laravel 5+:

php artisan make:migration add_sankar_to_tbl_admin_menu1

 

 

Here –>

add_ is prefix

sankar is column name

_to_  is        joiner

tbl_admin_menu1 is existing table name

 

use $table->integer('sankar')->after(whichever_column); to add this field after specific column.

 

 

 

Creating Migration file in Laravel Project

Migrations are like version control for your database, allowing your team to easily modify and share the application’s database schema. 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.

The Laravel Schema facade provides database agnostic support for creating and manipulating tables across all of Laravel’s supported database systems.

 

Generating Migrations

To create a migration, use the make:migration Artisan command:

 

php artisan make:migration create_users_table

 

The new migration will be placed in your database/migrations directory. Each migration file name contains a timestamp which allows Laravel to determine the order of the migrations.

The --table and --create options may also be used to indicate the name of the table and whether the migration will be creating a new table. These options simply pre-fill the generated migration stub file with the specified table:

 

php artisan make:migration create_users_table --create=users

php artisan make:migration add_votes_to_users_table --table=users

If you would like to specify a custom output path for the generated migration, you may use the --path option when executing the make:migration command. The given path should be relative to your application’s base path.

 

Migration Structure

A migration class contains two methods: up and down. The up method is used to add new tables, columns, or indexes to your database, while the down method should simply reverse the operations performed by the up method.

Within both of these methods you may use the Laravel schema builder to expressively create and modify tables. To learn about all of the methods available on the Schema builder, check out its documentation. For example, this migration example creates a flights table:

<?php

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

class CreateFlightsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('flights', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('airline');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('flights');
    }
}

 

Column type Available method (function) for Database column type like Int, Varchar, long string, bool etc.

Running Migrations

To run all of your outstanding migrations, execute the migrate Artisan command:

php artisan migrate 

Forcing Migrations To Run In Production

Some migration operations are destructive, which means they may cause you to lose data. In order to protect you from running these commands against your production database, you will be prompted for confirmation before the commands are executed. To force the commands to run without a prompt, use the --force flag:

php artisan migrate --force

Rolling Back Migrations

To rollback the latest migration operation, you may use the rollback command. This command rolls back the last “batch” of migrations, which may include multiple migration files:

php artisan migrate:rollback

You may rollback a limited number of migrations by providing the step option to the rollback command. For example, the following command will rollback the last five migrations:

php artisan migrate:rollback --step=5

How to Create a Model in Laravel

In the Laravel project we can make Model file by Command line or making by inner to model folder.

This is code for making Model (class) with named Widget:

We’re going to tackle something really basic by making a Widget model that we can use for future tutorials. I’m calling it widget so it can describe anything generic. This will come in really handy and you will love how easy and intuitive creating a model is in Laravel 5.1*.

Let’s start by going to the command line and running:

php artisan make:model Widget -m
 

Obviously we are using the make command in artisan and you can see how intuitive this really is. You’ll note that we added the flag -m, so that it will also make a migration for us. This is a super optimized workflow, gotta love that.

In your app folder, you will now see a Widget.php file:



<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Widget extends Model
{
   //
}

And that’s all there is to that!

So let’s go to our migration, which is located in database/migrations, you’ll see something like:



2015_08_171507_create_widgets_table.php

The date time is prepended to the descriptive name of the migration. You should note that Laravel expects a singular name for the model and plural name for the actual DB table.

This is very intuitive because you can think of the model returning a single instance of the tables records, so when we want a widget, we look in the widgets table.

Ok, so with migrations, you a nice stub, ready for you:



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

class CreateWidgetsTable extends Migration
{
   /**
    * Run the migrations.
    *
    * @return void
    */
   public function up()
   {
       Schema::create('widgets', function (Blueprint $table) {
           $table->increments('id');
           $table->timestamps();
       });
   }

   /**
    * Reverse the migrations.
    *
    * @return void
    */
   public function down()
   {
       Schema::drop('widgets');
   }
}

Obviously you are getting the auto-incrementing id column as well as the timestamps, so this is a great timesaver.

So we’re just going to add one column to the up method to keep this simple:



$table->string('widget_name')->unique();

You’ll note that I’m calling it widget_name instead of name. The reason I like to do this is to keep the column name clear. Imagine you have 10 different models, all with a name column. This makes it harder to work with when you are doing joins, so I take the extra step to prepend the modelname_ to the column name. This is not a hard-baked rule, but I like to follow this convention.

Also note, that we put a unique constraint on it. So now your up method should look like this:



public function up()
{
   Schema::create('widgets', function (Blueprint $table) {
       $table->increments('id');
       $table->string('widget_name')->unique();
       $table->timestamps();
   });
}

Now let’s run the migration from the command line:



php artisan migrate

And with that you should have your new table.

Since we’re planning to add records to the widgets table, we need to add the following property to our Widget model:



/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['widget_name'];

This tells eloquent that this is a mass assignable property, and if we didn’t have this, Laravel’s Eloquent wouldn’t allow us to save any records.

And that’s all there is to making a model in Laravel. In our next tutorial, we will use Laravel’s model factory to populate some test records. Then, in other tutorials, we will make the routes, controllers and views, so we can play with our widgets.

I hope you have enjoyed this tutorial and found it useful. Click on the sprocket icon at the top of the page to see all tutorials. Please comment, share, and like if you can, thanks.

Facebook logout from all device

Sometimes you may got Facebook logout forget issue in your life.  You may have logged in facebook with others mobile/ Tablet, or Laptop or other devices but forget to logout. It is not hacking issue but memorization of technical problem.

This time you have two choices

  1. one is request to logout for relevent person.
  2. Logout remember password/ from all device and change its password.

In the case of changing password of your facebook account. You must remember to force logout from all devices. It means logout from Facebook Mobile application, Browser and other devices.

 

To log out of Facebook on another computer, phone or tablet:

  1. Go to your Security and Login Settings.
  2. Click Where You’re Logged In.
  3. Find the session you want to end and click End Activity.

 

Step 1:

 

Step 2:

 

Step 3:

You have two option for logout previous device. One by one or make all device logout.

For logout from all device click on the link below the page Log Out Of All Sessions

 

 

 

https://www.facebook.com/help/211990645501187?helpref=faq_content