0

I'm trying to Import an excel file and save the info into the database table. Right now I'm getting an error

The POST method is not supported for this route. Supported methods: GET, HEAD, PUT, PATCH, DELETE.

All the tutorials I saw didn't get this error and I'm doing it the same way, I don't know what the issue is. I'm using this package "maatwebsite/excel": "~2.1.3".

This is my vue

<el-form :action="'impteachers/import'" method="post" enctype="multipart/form-data">
    <el-input type="file" name="file"/>
    <el-input type="submit" value="upload"/>
</el-form>

This is the controller

public function import(Request $request)
{
    $request->validate([
        'file' => 'required|mimes:xls,xlsx'
    ]);

    $path = $request->file('file')->getRealPath();
    $data = Excel::load($path)->get();

    if ($data->count()) {
        foreach ($data as $key => $value){
            $arr[] = [
                'NOMBRE' => $value->name,
                'CEDULA' => $value->card,
                'CARNET' => $value->scard,
                'TIPO-USUARIO' => $value->user_type_id,
                'CORREO' => $value->email,
                'PASSWORD' => $value->password,
            ];
        }

        if (!empty($arr)) {
            User::insert($arr);
        }
    }
    return redirect('/imports');
}

These are the routes in web.php

Route::resource('impteachers', 'ImportTeacherController');
Route::post('impteachers/import', 'ImportTeacherController@import');

What am I doing wrong?

Nancy
  • 1,021
  • 4
  • 23
  • 45

1 Answers1

1

Try submitting to '/impteachers' only like this

<el-form :action="'/impteachers'" method="post" enctype="multipart/form-data">
    <el-input type="file" name="file"/>
    <el-input type="submit" value="upload"/>
</el-form>

And set your route to this

Route::post('/impteachers', 'ImportTeacherController@import');
TJ Weems
  • 1,086
  • 3
  • 21
  • 37
  • That was a copy/paste mistake, I do have them both as import in my project – Nancy Oct 29 '19 at 17:39
  • Oh okay. Well I would update your question so others don't try to correct the same thing. Maybe try remove the `/import` and try submitting to `/impteachers` only. I will update my answer. – TJ Weems Oct 29 '19 at 17:47
  • I tried this, but now I'm getting a `419 Page Expired` – Nancy Oct 29 '19 at 18:54
  • @Nancy, 419 typcially has to do with csrf_token. check this [article](https://stackoverflow.com/questions/46472812/ajax-laravel-419-post-error) to learn more. – TJ Weems Oct 29 '19 at 18:58
  • I have both `` and `@csrf` in my head, and if I add anything related to `csrf` in my form the page doesn't load, no error but not loading. – Nancy Oct 29 '19 at 19:18
  • @Nancy, well I suggest making a new stackover flow question regarding the 419 alarm when submitting your form since it is different from you original issue. – TJ Weems Oct 29 '19 at 19:29
  • Thanks, I think I've resolved it (for now at least) – Nancy Oct 29 '19 at 19:33
  • Awesome, if my answer fixed your original issue could please check it for me? Im trying to get these points lol – TJ Weems Oct 29 '19 at 19:37