Laravel 5.3使用restfull controller

时间:2017-01-28 17:54:29

标签: php laravel-5

我正在尝试使用资源控制器向数据库添加记录,但是,我收到了MethodNotAllowedHttpException错误。我正在使用数据透视表。我已经经历了几个类似的问题,比如a link!但似乎没有人回答我。这是我的代码:

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');
        }
     }

这是表格:

  <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="{{ $section->id }}">{{ $section->name }}</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="{{ csrf_token() }}">
        <button type="submit" class="btn btn-default">Submit</button>
        </form>

这是型号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');
       }
    }

这是模型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');
       }
    }

当我在上述路线下添加此路线时

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

然后一切都像魅力一样。我是Laravel的新手,但我认为一切都必须适用于那条路线,因为我使用了restfull控制器。我会嘲笑任何吸烟。谢谢。

2 个答案:

答案 0 :(得分:0)

将表单操作更改为&#34; / user / categories&#34;喜欢

<form action="/user/categories" method="POST">

答案 1 :(得分:0)

最后,我找到了答案。根据RESTfull资源控制器,表单操作的URL必须是主路由,而不是/ store或/ create。所以在表格行动中我写道:

  <form action="/user/categories" method="POST">

这对我有用。无论如何,谢谢你的帮助。我希望这会对某人有所帮助。