mardi 18 octobre 2016

MethodNotAllowedHttpException on a put request laravel

Been following some documentation on laravel restful controllers and have one set up to do basic editing and adding of items to a database. It was going well till I hit the snag on... well I'm not sure what precisely is triggering the problem, but basically, everything works till I hit submit on the form and then it's Game Over.

Normally I'd be able to diagnose this by checking to see if I'm using the right call, or made a spelling mistake or something. But this is a new request for me, so I can't quite debug where the problem is coming from.

This is the error in full for those who know what to look for.

MethodNotAllowedHttpException in RouteCollection.php line 218:

        in RouteCollection.php line 218
        at RouteCollection->methodNotAllowed(array('GET', 'HEAD', 'POST')) in RouteCollection.php line 205
        at RouteCollection->getRouteForMethods(object(Request), array('GET', 'HEAD', 'POST')) in RouteCollection.php line 158
        at RouteCollection->match(object(Request)) in Router.php line 780
        at Router->findRoute(object(Request)) in Router.php line 610
        at Router->dispatchToRoute(object(Request)) in Router.php line 596
        at Router->dispatch(object(Request)) in Kernel.php line 267
        at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 53
        at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
        at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 137
        at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
        at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 104
        at Pipeline->then(object(Closure)) in Kernel.php line 149
        at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 116
        at Kernel->handle(object(Request)) in index.php line 53
        at require_once('C:\xampp\htdocs\Missionseek2\public\index.php') in server.php line 21

My routes are as follows:

Route::get('NewUser', 'UserEntryController@create');
Route::post('NewUser', 'UserEntryController@UserForm');
Route::get('User/', 'UserEntryController@index')->name('User');
Route::post('NewUser', 'UserEntryController@UserForm')->name('test');
Route::resource('users', 'EditUserController');

Controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests\ContactFormRequest;
use App\UserEdit;
use DB;
use App\Http\Requests;

class EditUserController extends Controller
{

    public function index()
    {
        $array = UserEdit::all()->toArray();
        return view('UserEntry', compact('array'));
    }


    public function create()
    {
        $id = UserEdit::find(715)->toArray();
        return view('NewUser', compact('id'));
    }


    public function store(UserFormRequest $request)
    {
        //$user = new UserEdit([
        //    'name'=>$request->get('First_Name'),
        //    'email'=>$request->get('email'),
        //    'username'=>$request->get('name')
        //]);
        //
        //$user->save();
        //return \Redirect::route('users')->with('message', 'Nice Work.');
    }


    public function show($id)
    {
        try {
         $array = UserEdit::findorFail($id)->toArray();
         return view('UserEdit')->with('array', $array);

         } catch(\Exception $e) {
             return \Redirect::route('users.index')
                ->withMessage('This user does not exist');
         }

    }


    public function edit($id)
    {
        $user = UserEdit::findorFail($id);
        return view('EditUser')->with('user',$user);
    }


    public function update($id, UserFormRequest $request)
    {
       $user = UserEdit::findorFail($id);

       $user->update([
           'name' => $request->get('name'),
           'email' => $request->get('email')
       ]);

       return \Redirect::route('users.edit', [$user->id])->with('message', 'Details Updated!');
    }


    public function destroy($id)
    {
        //
    }
}

Blade:

@extends('layout')


@section('content')
    <h1>This is a test.</h1>
    <ul>
        @foreach($errors->all() as $error)
            <li></li>
        @endforeach
    </ul>
    {!! Form::model($user, ['method'=>'put', 'route'=>['users.update', $user->id], 'class'=>'form']) !!}
    <div class="form-group">
         
        {!! Form::label('Your Name') !!}
        {!! Form::text('name', null, 
            ['required', 'class' => 'form-control', 'placeholder'=>'Your name']
        ) !!}
    </div>

    <div class="form-group">
        {!! Form::label('Your E-mail Address') !!}
        {!! Form::text('email', null, 
            ['required', 'class' => 'form-control', 'placeholder'=>'Your E-mail Address']
        ) !!}
    </div>

    <div class="form-group">
        {!! Form::submit('Submit', ['class'=>'btn btn-primary']) !!}
    </div>
    {!! Form::close() !!}

@stop



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire