我正在尝试为客户端创建一个简单的CRUD。目前我可以查看客户端并进入编辑页面。但是,如果我尝试更新或创建客户端,我会得到一个MethodNotAllowedHttpException。
我的路线如下所示
Route::model('clients', 'Client');
Route::bind('clients', function($value, $route) {
return App\Client::whereSlug($value)->first();
});
Route::resource('clients', 'ClientsController');
我的控制器是这样的
<?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.');
}
}
然后我有一个像这样的表格
<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>
我的edit.blade.php就像这样
<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() !!}
我已经研究过这个错误,很多人都提到了javascript,但我目前没有使用任何javascript。
为什么我会收到此错误?正如我所说,当我尝试创建一个客户端时,我会得到它。
由于
作为更新,如果我删除部分表单并将其添加到ed.it.blade.php,它似乎可以正常工作
{!! 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() !!}
为什么?