jeudi 20 août 2015

Laravel 5 MethodNotAllowedHttpException issues

I am trying to make a simple CRUD for Clients. At the moment I can view clients and get to the edit page. However, if I try to update or create a client, I get a MethodNotAllowedHttpException.

My routes look like the following

Route::model('clients', 'Client');

Route::bind('clients', function($value, $route) {
    return App\Client::whereSlug($value)->first();
});

Route::resource('clients', 'ClientsController');

My controller is like so

<?php

namespace App\Http\Controllers;

use App\Client;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Input;
use Redirect;

use Illuminate\Http\Request;

class ClientsController extends Controller {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $clients = Client::all();
        return view('clients.index', compact('clients'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        return view('clients.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        $input = Input::all();
        Client::create( $input );

        return Redirect::route('clients.index')->with('message', 'Client created');
    }


    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Client $client
     * @return Response
     */
    public function edit(Client $client)
    {
        return view('clients.edit', compact('client'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \App\Client $client
     * @return Response
     */
    public function update(Client $client)
    {
        $input = array_except(Input::all(), '_method');
        $client->update($input);

        return Redirect::route('clients.index', $client->slug)->with('message', 'Client updated.');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Client $client
     * @return Response
     */
    public function destroy(Client $client)
    {
        $client->delete();

        return Redirect::route('clients.index')->with('message', 'Client deleted.');
    }

}

I then have a form partial like so

<div class="form-group">
    {!! Form::label('clientName', 'Client Name:', array('class' => 'col-sm-5 control-label blue')) !!}
    <div class="col-sm-7">
        {!! Form::text('clientName', null, array('class' => 'form-control')) !!}
    </div>
</div>
<div class="form-group">
    {!! Form::label('contactEmail', 'Contact Email:', array('class' => 'col-sm-5 control-label blue')) !!}
    <div class="col-sm-7">
        {!! Form::text('contactEmail', null, array('class' => 'form-control')) !!}
    </div>
</div>
<div class="form-group">
    {!! Form::label('slug', 'Slug:', array('class' => 'col-sm-5 control-label blue')) !!}
    <div class="col-sm-7">
        {!! Form::text('slug', null, array('class' => 'form-control')) !!}
    </div>
</div>
<div class="form-group">
    {!! Form::submit($submit_text, ['class'=>'btn btn-default']) !!}
</div>

And my edit.blade.php is like so

<h2>Edit Client</h2>
{!! Form::model($client, ['class'=>'form-horizontal'],  ['method' => 'PATCH', 'route' => ['clients.update', $client->slug]]) !!}
    @include('clients/partials/_form', ['submit_text' => 'Edit Client'])
{!! Form::close() !!}

I have researched this error and a lot of people refer to javascript, but I am not using any javascript at the moment.

Why would I be getting this error? As I say, I get it when I try to create a client as well.

Thanks

As an update, if I remove the form partial and add this to ed.it.blade.php instead, it seems to work

{!! Form::model($client, [
'method' => 'PATCH',
'route' => ['clients.update', $client->slug]
]) !!}

<div class="form-group">
    {!! Form::label('clientName', 'Client Name:', array('class' => 'col-sm-5 control-label blue')) !!}
    <div class="col-sm-7">
        {!! Form::text('clientName', null, array('class' => 'form-control')) !!}
    </div>
</div>
<div class="form-group">
    {!! Form::label('contactEmail', 'Contact Email:', array('class' => 'col-sm-5 control-label blue')) !!}
    <div class="col-sm-7">
        {!! Form::text('contactEmail', null, array('class' => 'form-control')) !!}
    </div>
</div>
<div class="form-group">
    {!! Form::label('slug', 'Slug:', array('class' => 'col-sm-5 control-label blue')) !!}
    <div class="col-sm-7">
        {!! Form::text('slug', null, array('class' => 'form-control')) !!}
    </div>
</div>

{!! Form::submit('Update Client', ['class' => 'btn btn-primary']) !!}

{!! Form::close() !!}

Why is that?

laravel php laravel 5 laravel 4 laravel with laravel tutorial

Aucun commentaire:

Enregistrer un commentaire