title: Add Sub Category - Laravel 5.2
body: <p>I need to insert sub categories into my main categories. I have already done all the work for Showing, Adding, Editing, and Deleting parent Categories. But now im stuck with how to actually add a sub category to one of my parents category.</p>
  
  <p>Here is how the table looks like for the category's and sub categories.</p>
  
  <p><a href="https://i.stack.imgur.com/dHJa3.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dHJa3.jpg" alt="Categories Table"></a></p>
  
  <p>As you can see I already Have a sub category under iPhone's, which I added manually through the database. To add a sub category to a main category, I just click on the + sub-Category link which takes me to the form to add sub category.</p>
  
  <p>Here i my route to show and add a sub category:</p>
  
  <pre class="lang-html prettyprint-override"><code>Route::group(["middleware" =&gt; 'admin'], function(){
  
      /** More category routes here --&gt;, just hidden for shortness **/
  
  
      /** Show the Admin Add Sub-Categories Page **/
      Route::get('admin/categories/addsub/{id}', [
          'uses' =&gt; '\App\Http\Controllers\CategoriesController@addSubCategories',
          'as'   =&gt; 'admin.category.addsub',
          'middleware' =&gt; ['auth'],
      ]);
  
  
      /** Post the Sub-Category Route **/
      Route::post('admin/categories/postsub/{id}', [
          'uses' =&gt; '\App\Http\Controllers\CategoriesController@addPostSubCategories',
          'as'   =&gt; 'admin.category.postsub',
          'middleware' =&gt; ['auth'],
      ]);
  
  
  });
  </code></pre>
  
  
  
  <p>Here is my CategoriesController.php:</p>
  
  <p>It is shortened just to show sub-categories functions. This is where I'm having trouble adding a sub category to a parent category</p>
  
  <pre class="lang-html prettyprint-override"><code>class CategoriesController extends Controller
      /**
       * Return the view for add new sub category
       *
       * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
       */
      public function addSubCategories($id) {
  
          $category = Category::findOrFail($id);
  
          return view('admin.category.addsub', compact('category'));
      }
  
  
      /**
       * @param $id
       * @param CategoryRequest $request
       * @return \Illuminate\Http\RedirectResponse
       */
      public function addPostSubCategories($id, CategoryRequest $request) {
  
          // Find the Parent Category ID
          $category = Category::findOrFail($id);
  
          // Insert into categories where the Parent_id = to the category ID
          $categories = Category::where('parent_id', '=', $category);
  
  
          // Assign $category to the Category Model, and request all validation rules
          $categories = new Category($request-&gt;all());
  
          // Then save the newly created category in DB
          $categories-&gt;save();
  
          // Flash a success message
          flash()-&gt;success('Success', 'Sub Category added successfully!');
  
          // Redirect back to Show all categories page.
          return redirect()-&gt;route('admin.category.show');
      }
  
  }
  </code></pre>
  
  
  
  <p>My Category.php Model:</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Category extends Model
  {
  
      protected $table = 'categories';
  
      protected $fillable = ['name'];
  
      protected $guarded = ['id'];
  
  
      public function parent() {
          return $this-&gt;belongsTo('App\Category', 'parent_id');
      }
  
  
  
      public function children() {
          return $this-&gt;hasMany('App\Category', 'parent_id');
      }
  
  }
  </code></pre>
  
  
  
  <p>My add sub category form:</p>
  
  <pre class="lang-html prettyprint-override"><code> &lt;form role="form" method="POST" action="{{ route('admin.category.postsub', $category-&gt;id) }}"&gt;
                      {{ csrf_field() }}
                      &lt;li class="collection-item blue"&gt;
                          &lt;h5 class="white-text text-center"&gt;
                              Sub-Category to {{ $category-&gt;name }}
                          &lt;/h5&gt;
                      &lt;/li&gt;
                      &lt;li class="collection-item"&gt;
                          &lt;div class="form-group{{ $errors-&gt;has('name') ? ' has-error' : '' }}"&gt;
                        &lt;input type="text" class="form-control" name="name" value="{{ old('name') }}" placeholder="Add Sub-Category"&gt;
                              @if($errors-&gt;has('name'))
                                  &lt;span class="help-block"&gt;{{ $errors-&gt;first('name') }}&lt;/span&gt;
                              @endif
                          &lt;/div&gt;
                      &lt;/li&gt;
                      &lt;li class="collection-item blue"&gt;
                          &lt;div class="form-group text-center"&gt;
                              &lt;button type="submit" class="btn btn-link grey lighten-5"&gt;Create Sub-Category&lt;/button&gt;
                          &lt;/div&gt;
                      &lt;/li&gt;
                  &lt;/form&gt;
  </code></pre>
  
  
  
  <p>And my Database structure:</p>
  
  <p><a href="https://i.stack.imgur.com/9Y0Bi.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9Y0Bi.jpg" alt="My DB structure"></a></p>
  
  <p>I particulary need help in my addPostSubCategories() function in my CategoriesController, because right now if I add a new SUB category, it just adds a new Parent Category, not a sub category</p>