【问题标题】:MethodNotAllowedHttpException laravel 5.4MethodNotAllowedHttpException laravel 5.4
【发布时间】:2018-07-11 01:45:12
【问题描述】:

尝试进行简单插入,但我在 RouteCollection->methodNotAllowed(array('GET', 'HEAD', 'PUT', 'PATCH', 'DELETE' )) 在 RouteCollection.php 中(第 238 行)

这是我在视图中的表单

  <form method="post" action="{{route('product.create')}}" class="form-horizontal" enctype="multipart/form-data">
        {!! csrf_field() !!}
        <fieldset>
            <!-- Text input-->
            <div class="form-group">
                <label class="col-md-3 control-label" for="name">Name</label>
                <div class="col-md-9">
                    <input id="name" name="name" type="text" placeholder="Product name" class="form-control input-md">

                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3 control-label" for="textarea">Description</label>
                <div class="col-md-9">
                    <textarea class="form-control" id="textarea" name="description"></textarea>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3 control-label" for="size">Size</label>
                <div class="col-md-9">
                    <select class="form-control" id="size">
                        <option selected>Choose size...</option>
                        <option value="small">Small</option>
                        <option value="medium">Medium</option>
                        <option value="larg">Larg</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3 control-label" for="category_id">Category</label>
                <div class="col-md-9">
                    <select class="form-control" id="category_id">

                        <option selected>Choose Categories...</option>
                        {{--<option value= "$categories"></option>--}}
                        <option value= "1"> men </option>

                    </select>
                </div>
            </div>

            <div class="form-group">
                <label class="col-md-3 control-label" for="image">Image</label>
                <div class="col-md-9">
                    <input id="file" name="image" class="input-file" type="file">
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3 control-label" for="submit"></label>
                <div class="col-md-9">
                    <button id="submit" name="submit" class="btn btn-primary">Create</button>
                </div>
            </div>

        </fieldset>

    </form>

我的路线

Route::group(['prefix'=>'admin','middleware'=>'auth'],function(){

Route::get('/' ,function(){
return view('admin.index');
})->name('admin.index');
});
Route::resource('product','ProductsController');
Route::resource('category','CategoriesController');

我的控制器

public function create()
{
$categories = Category::pluck('name','id');
return view('admin.product.create',compact('categories'));
}

public function store(Request $request)
{
$formInput = $request->except('image');
$image = $request->image;

if($image){

    $imageName = $image->getClientOriginalName();
    $image->move('images', $imageName);

    $formInput['image']=$imageName;
  }

 Product::create($formInput);
 return redirect()->route('admin.index');

}

任何帮助将不胜感激

【问题讨论】:

    标签: laravel-5.4


    【解决方案1】:

    应该是:

    action="{{route('product.store')}}"
    

    路由 product.create 需要 GET 标头来显示注册表单

    【讨论】:

    • 感谢您的重播,我更改了操作,现在我没有收到错误,但它没有在我的数据库中存储任何建议?
    • 您的表单请求似乎有错误。看看您的控制器是否正在获取所有预期数据。提示:尝试使用控制器而不是资源控制器。最后一个被广泛用于创建 API
    • 我提交表单时得到的 dd($request); +request: ParameterBag {#41 ▼ #parameters: array:4 [▼ "_token" => "2wDgwkQgH8vVmo283Aod4WbDSsHd5o3Ev7Gx8e57" "name" => "test name" "description" => "test DescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescription" "submit" => null ] }
    • 我只得到 2 个结果,我的表​​单中有更多尺寸、图像、类别和图像
    【解决方案2】:

    添加名称后,我现在忘记在表单中添加名称,我得到除图像以外的所有字段

    【讨论】:

      【解决方案3】:

      我尝试了以上所有答案,但仍然遇到这个问题。所以我发现我的模型需要“可填充”属性:

      protected $fillable = ['name', 'status', 'etc','etc2'];
      

      这解决了问题,现在我可以发布/放置/删除了。

      另外,在我的路由文件中,我没有对每个操作使用一个路由,而是简单地使用,例如:

       Route::resource('user', 'v1\UserController');
      

      【讨论】:

        猜你喜欢
        • 2017-12-21
        • 2018-02-28
        • 1970-01-01
        • 2017-08-03
        • 2018-04-13
        • 1970-01-01
        • 2018-05-28
        • 2018-07-19
        • 1970-01-01
        相关资源
        最近更新 更多