samedi 28 janvier 2017

Laravel 5.3 throw MethodNotAllowedHttpException on Store using restfull controller

I am trying to add a record to a database utilizing a resource controller, however, I'm getting MethodNotAllowedHttpException error. I'm using a pivot table. I have gone through several similar questions like a link! but none seem to answer me. This is my code:

Routes.php

 Route::group(['prefix' => 'user'], function () {
   Route::resource('categories', 'User\CategoriesController');
 });

CategoriesController.php

  <?php

    namespace App\Http\Controllers\User;
    use Session;
    use Sentinel;
    use Illuminate\Http\Request;
    use App\Http\Controllers\Controller;
    use App\Models\Sections;
    use App\Models\Categories;
    use App\Models\Users;
    use App\Models\CategorieUser;

    class CategoriesController extends Controller
    {
      public function __construct()
      {

       $this->middleware('sentinel.auth');
      }

      public function index()

      {
       $categories = Categories::all();
       return view('user.categories.index', ['categories' => $categories]);
      }

      public function create()
      {
       $sections = sections::all();
       return view('user.categories.create', ['sections' => $sections]);
      }

      public function store(Request $request)
      {
       // records in table categories
       $categories = new Categories();
       $categories->name = $request->name;
       $categories->sections_id = $request->sections_id;
       $categories->save();

        // records in pivot table users_categories
        $user = Sentinel::getUser()->id;
        $users_categories = new CategorieUser();
        $users_categories->user_id = $user;
        $users_categories->categorie_id = $categories->id;
        $users_categories->save();
        return redirect()->route('categories.index');
        }
     }

This is the form:

  <form action="store" method="POST">

        <div class="form-group">

        <label for="section">Choose section:</label>

        <select class="form-control" name="sections_id">
        @foreach($sections as $section)
                    <option value=""></option>
        @endforeach
           </select>
       </div>


       <div class="form-group">
       <label for="name">Category name:</label>

       <input type="text" class="form-control" name="name" required>


        </div>
        <input type="hidden" name="_token" value="">
        <button type="submit" class="btn btn-default">Submit</button>
        </form>

This is model Categories.php

  <?php

    namespace App\Models;
    use Illuminate\Database\Eloquent\Model;

    class Categories extends Model
     {
       public function sections()
       {
       return $this->belongsTo('App\Models\Sections');
       }

       public function users()
       {
      return $this->belongsToMany('App\Models\Users', 'categorie_user');
       }
    }

And this is model Users.php:

  <?php

    namespace App\Models;
    use Illuminate\Database\Eloquent\Model;

    class Users extends Model
    {
     public function categories()
       {
     return $this->belongsToMany('App\Models\Categories', 'categorie_user');
       }
    }

When I add this route under routes as above

  Route::group(['prefix' => 'user'], function () {
   Route::resource('categories', 'User\CategoriesController');
   Route::post('categories/store', ['uses' => 'User\CategoriesController@store']);
  });

then everything works like a charm. I'm newbie in Laravel but I think that everything must work with out that route because I use restfull controller. Any sugestions I will appriciated. Thank you.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire