简体   繁体   中英

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

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 . But, here is nothing to deal with library . You need to submit the form to store() method in Mian controller.

Change action="library" to action="{{ action('Main@store') }}" in form starting tag.

add this route='library.store' to your form:

<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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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