[英]Laravel error: MethodNotAllowedHttpException in RouteCollection.php line 219
When i want to insert data in the database i get this error当我想在数据库中插入数据时出现此错误
MethodNotAllowedHttpException in RouteCollection.php line 219 RouteCollection.php 第 219 行中的 MethodNotAllowedHttpException
I'm use resource controller我正在使用资源控制器
this is my form这是我的表格
<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>
and this is my store function这是我的商店功能
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');
}
and this my Route这是我的路线
Route::resource('library', 'Main');
You are using action="library"
, so the form is submitted to library
.您正在使用action="library"
,因此表单被提交到library
。 But, here is nothing to deal with library
.但是,这里与library
无关。 You need to submit the form to store()
method in Mian
controller.您需要将表单提交到Mian
控制器中的store()
方法。
Change action="library"
to action="{{ action('Main@store') }}"
in form starting tag. action="{{ action('Main@store') }}"
表单起始标记中的action="library"
更改为action="{{ action('Main@store') }}"
。
add this route='library.store'
to your form:将此route='library.store'
添加到您的表单中:
<form method="POST" route="library.store" enctype="multipart/form-data" files="true">
and your rout should be:你的路线应该是:
Route::resource('library', 'controller_class_name');
Change you route to:将您的路线更改为:
Route::resource('library', 'MainController');
Also, check out your controller.另外,请检查您的控制器。 It should be placed into app\\Http\\Controllers
directory, named MainController.php
and it should include this code:它应该放在app\\Http\\Controllers
目录中,命名为MainController.php
并且它应该包含以下代码:
class MainController extends Controller
{
....
public function store(Request $request)
....
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.