add another column in a table in laravel

Solutions on MaxInterview for add another column in a table in laravel by the best coders in the world

showing results for - "add another column in a table in laravel"
Latoya
27 Sep 2018
1<?php
2
3use Illuminate\Support\Facades\Schema;
4use Illuminate\Database\Schema\Blueprint;
5use Illuminate\Database\Migrations\Migration;
6
7class AddStoreIdToUsersTable extends Migration
8{
9    /**
10     * Run the migrations.
11     *
12     * @return void
13     */
14    public function up()
15    {
16        Schema::table('users', function (Blueprint $table) {
17
18            // 1. Create new column
19            // You probably want to make the new column nullable
20            $table->integer('store_id')->unsigned()->nullable()->after('password');
21
22            // 2. Create foreign key constraints
23            $table->foreign('store_id')->references('id')->on('stores')->onDelete('SET NULL');
24        });
25    }
26
27    /**
28     * Reverse the migrations.
29     *
30     * @return void
31     */
32    public function down()
33    {
34        Schema::table('users', function (Blueprint $table) {
35
36            // 1. Drop foreign key constraints
37            $table->dropForeign(['store_id']);
38
39            // 2. Drop the column
40            $table->dropColumn('store_id');
41        });
42    }
43}
44