简体   繁体   English

Laravel 5.1:如何为更新记录设置路由

[英]Laravel 5.1: How to set Route for update record

I am working with laravel 5.1 我正在使用laravel 5.1

I am using the routes of laravel. 我正在使用laravel路线。

I used Form/Html for 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? 我究竟做错了什么?

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 eg 然后在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! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM