title: Schema table in Laravel add column dynamically
body: <p>I have a dynamic add product form in which I access field and category related to type .</p>
  
  <ol>
  <li>fields table have fieldname and id</li>
  <li>type table have title.
  field and type have mane to many relation in model</li>
  <li>category table have category name and type id</li>
  <li>product table have category id and attributes related to product.</li>
  </ol>
  
  <p>I add product on the basis of type id if type_id is 1 then type_id 1 related fields and category show on add product form. </p>
  
  <p>Now problem is that when first time  URL load field list of type_id show but when second time, check only first field if product table have that field then other field skipped.</p>
  
  <pre><code>public function create($id){
      $type = Type::find($id);
      $category = Category::all();
      foreach ($type-&gt;field as $key =&gt; $value) {
          if ( Schema::hasColumn('products', "{$value-&gt;fieldname}") ) {
              return view('products.create', compact('type', 'category'));
          } else {
              if( ! Schema::hasColumn('products', "{$value-&gt;fieldname}") ) {
                  foreach ($type-&gt;field as $key =&gt; $value) {
                      Schema::table('products', function ($table) use ($value) {
                          $table-&gt;string("{$value-&gt;fieldname}")-&gt;nullable()-&gt; after('category_id');
                      });
                  }
  
                  return view('products.create', compact('type', 'category'));
              }
  
          }
  
      }
  }
  </code></pre>