php Laravel 5.1:如何为更新记录设置路由
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35036192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.1: How to set Route for update record
提问by Dhara
I am working with laravel 5.1
我正在使用 laravel 5.1
I am using the routes of laravel.
我正在使用laravel的路线。
I used Form/Htmlfor insert/update, but stuck in routing of update record.
我使用Form/Html进行插入/更新,但卡在更新记录的路由中。
Here is route for redirect to edit page in routes.php
这是重定向到routes.php中编辑页面的路由
Route::get('/company/edit/{id}','CompanyMasterController@edit');
In my CompanyMasterController.php
在我的 CompanyMasterController.php
public function edit($id)
{
$company = CompanyMasters::find($id);
return view('companymaster.edit', compact('company'));
}
My action in edit.blade.php
我在 edit.blade.php 中的操作
{!! Form::model($company,['method' => 'PATCH','action'=>['CompanyMasterController@update','id'=>$company->id]]) !!}
and route for this action in routes.php
并在routes.php中为此操作路由
Route::put('/company/update/{id}','CompanyMasterController@update');
My controller action for update.
我的控制器更新操作。
public function update($id)
{
$bookUpdate=Request::all();
$book= CompanyMasters::find($id);
$book->update($bookUpdate);
return redirect('/company/index');
}
Now when I click on submit button it gives me:
现在,当我点击提交按钮时,它给了我:
MethodNotAllowedHttpException in RouteCollection.php
RouteCollection.php 中的 MethodNotAllowedHttpException
What am I doing wrong?
我究竟做错了什么?
回答by Rwd
The main reason you're getting this error is because you set your form to submit with a PATCH
method and you've set your route to look for a PUT
method.
您收到此错误的主要原因是您将表单设置为使用PATCH
方法提交,并且您已将路由设置为查找PUT
方法。
The two initial options you have are either have the same method in your route file as your form or you could also set your route to:
您拥有的两个初始选项在您的路由文件中与您的表单具有相同的方法,或者您也可以将您的路由设置为:
Route::match(['put', 'patch'], '/company/update/{id}','CompanyMasterController@update');
The above will allow both methods to be used for that route.
以上将允许将这两种方法用于该路线。
Alternatively, you can use route:resource()
https://laravel.com/docs/5.2/controllers#restful-resource-controllers.
或者,您可以使用route:resource()
https://laravel.com/docs/5.2/controllers#restful-resource-controllers。
This will take care of all the basic Restful routes.
这将处理所有基本的 Restful 路由。
Then to take it one step further you can add the following to your routes file:
然后更进一步,您可以将以下内容添加到您的路由文件中:
Route::model('company', 'App\CompanyMasters'); //make sure the namespace is correct if you're not using the standard `App\ModelName`
Then your resource route would look something like:
然后你的资源路线看起来像:
Route::resource('company', 'CompanyMasterController');
And then in CompanyMasterController
your methods can be type hinted e.g.
然后在CompanyMasterController
你的方法中可以输入提示,例如
public function edit($id) {...}
Would become:
会成为:
public function edit(CompanyMaster $company)
{
return view('companymaster.edit', compact('company'));
}
Obviously, you don't have to go with this approach though if you don't want to.
显然,如果您不想,则不必采用这种方法。
Hope this helps!
希望这可以帮助!