0

I'm currently beginner of laravel. I'm studying laravel 5.2 from it's official docs. After studing migration in laravel, I'm very much clear about it's migration concept. But on practicing by scripting code, I come a problem. The problem is that as laravel tells laravel allows a team to easily modify and share the application's database schema. But how to change the structure of database table after once it is created through migration file of table. I found the solution here. But I have doubt on 8th step of the solution that If I will run that command, then all the migration fill will be executed. So that will give me error of Table is already exists. Am I correct? If yes, then explain with example like in that link. I think I have to run only last migrate file 2013_05_23_202930_update_users.php. If this is the answer, then also type command to run perticular single migrate file. If any one knows the answer, answer will be appreciated.

Akshay Vaghasiya
  • 1,597
  • 9
  • 36
  • 60

2 Answers2

2

To create table:

if (!Schema::hasTable('users')) {
        Schema::create('users', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->increments('id')->unsigned();
            $table->string('username'); 
            $table->string('password', 60);                
            $table->timestamps();
            $table->softDeletes();
        });
    }

To add some columns to this table:

php artisan make:migration add_somthing_to_users_table --table=users

Schema::table('users', function (Blueprint $table) {
        //
        if (!Schema::hasColumn('users', 'fb_id')) {
            //
            $table->string('fb_id')->default('');
        }
    });
rome 웃
  • 1,821
  • 3
  • 15
  • 33
  • So are you telling that when ever I have to update table, I should write code in create_users migration file, and then after run the same command as mentioned in 8th step of that link? As table is already created, so script for creating table will be skipped? And more that if is there any another way like separate migration file, then please also explain that. – Akshay Vaghasiya Feb 18 '16 at 05:10
  • bdw I'm satisfied with your answer. But laravel named migration file with time prefix, so I think that laravel is suggesting to create separate file for this type of task. So I'm telling you for alternate solution. – Akshay Vaghasiya Feb 18 '16 at 05:14
0

Okey guys, I found solution. After refering this link, I come to know that solution in posted link of question is correct. Due to some reason which I don't know, table was not getting changed. But after restarting my system, It works. But answer of @rome is also acceptable.

Akshay Vaghasiya
  • 1,597
  • 9
  • 36
  • 60