繁体   English   中英

Laravel 错误:RouteCollection.php 第 219 行中的 MethodNotAllowedHttpException

[英]Laravel error: MethodNotAllowedHttpException in RouteCollection.php line 219

当我想在数据库中插入数据时出现此错误

RouteCollection.php 第 219 行中的 MethodNotAllowedHttpException

我正在使用资源控制器

这是我的表格

<form action="library" method="POST" enctype="multipart/form-data">
    {!! csrf_field() !!}
        Enter the name of section: <input type="text" name="section_name"> <br>
        Upload an image: <input type="file" name="image"> <br>
        <button type="submit" class="btn btn-default">Create Section</button>
    </form>

这是我的商店功能

public function store(Request $request)
{

    $section_name = $request->input('section_name');
    $file = $request->file('image');
    $destenationPath = 'iamges';
    $filename = $file->getClientOriginalName();
    $file->move($destenationPath, $filename);
    DB::table('sections')->insert(['section_name' => $section_name, 'image_name' => $filename]);
    return redirect('admin');

}

这是我的路线

Route::resource('library', 'Main');

您正在使用action="library" ,因此表单被提交到library 但是,这里与library无关。 您需要将表单提交到Mian控制器中的store()方法。

action="{{ action('Main@store') }}"表单起始标记中的action="library"更改为action="{{ action('Main@store') }}"

将此route='library.store'添加到您的表单中:

<form  method="POST" route="library.store" enctype="multipart/form-data" files="true">

你的路线应该是:

Route::resource('library', 'controller_class_name');

将您的路线更改为:

Route::resource('library', 'MainController');

另外,请检查您的控制器。 它应该放在app\\Http\\Controllers目录中,命名为MainController.php并且它应该包含以下代码:

class MainController extends Controller
{
    ....
    public function store(Request $request)
    ....
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM