Laravel error: MethodNotAllowedHttpException in RouteCollection.php

After generating the code to download Excel, I am getting the below error ...

MethodNotAllowedHttpException in RouteCollection.php

The codes written in VIEW are

view / items / items

  @extends('layouts.master')

@section('content')
      <div class="row">
        <div class="col-md-4"></div>
        <div class="col-md-6">
          <div class="row">
<form action="{{route('items.import')}}" method="POST" enctype="multipart/form-data">
              <div class="col-md-6">
                {{csrf_field()}}
                <input type="file" name="imported-file"/>
              </div>
              <div class="col-md-6">
                  <button class="btn btn-primary" type="submit">Import</button>
              </div>
            </form>
          </div>
        </div>
        <div class="col-md-2">
         <!-- <button class="btn btn-success">Export</button> -->
        </div>
      </div>

    @endsection

      

The codes written in route.php are ...

Route::get('/items', 'ItemController@index');

Route::post('/items/import',[ 'as' => 'items.import', 'uses' => 'ItemController@import']);

      

ItemController.ASPX

 public function index()
    {
      return view('items.items');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
public function import(Request $request)
    {
      if($request->file('imported-file'))
      {
                $path = $request->file('imported-file')->getRealPath();
                $data = Excel::load($path, function($reader) {
            })->get();

            if(!empty($data) && $data->count())
      {
        $data = $data->toArray();
        for($i=0;$i<count($data);$i++)
        {
          $dataImported[] = $data[$i];
        }
            }
      Inventory::insert($dataImported);
        }
        return back();
  }

      

Can someone please help me what is missing in my encoding which is outputting the error ...

0


source to share


1 answer


Try this code instead of yours:

Route::post('/items/import',[ 'as' => 'items/import', 'uses' => 'ItemController@import']);

      

The trick is to name the route. To avoid confusion in the future, it is better to name it "items.import", so you can see for yourself later that this is the "name" of the route. So the final code is:



Route::post('/items/import',[ 'as' => 'items.import', 'uses' => 'ItemController@import']);

      

and in your clip template u call it like this:

<form action="{{route('items.import')}}"...

      

0


source







All Articles