laravel 缺少 App\Http\Controllers\ProductsController::edit() 的参数 1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42386186/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:23:29  来源:igfitidea点击:

Missing argument 1 for App\Http\Controllers\ProductsController::edit()

laravel

提问by Archie Rombo

Im working on editing of an already saved product and i am getting the following error message on my browser

我正在编辑已保存的产品,但在浏览器上收到以下错误消息

  ErrorException in ProductsController.php line 88:
    Missing argument 1 for App\Http\Controllers\ProductsController::edit()
    in ProductsController.php line 88

My route controller is as below:

我的路由控制器如下:

Route::get('productsedit', array('as'=> '/productsedit','uses'=>'ProductsController@edit'));

The function is as below

功能如下

public function edit($id)
    {
          //find the post in the db and sav it as a variable
        $product = Products:: findOrFail($id);

        //return view and pass in the var previously created 
        return view('/productsedit')->withProducts($product);
    }

Where iam i going wrong

我哪里出错了

回答by Christophvh

In your edit method on your ProductsController you require a parameter ($id), but you don't have that value in your route . Which is what this error is saying.

在您的 ProductsController 上的编辑方法中,您需要一个参数 ( $id),但您的 route 中没有该值。这就是这个错误所说的。

[Missing argument 1 for App\Http\Controllers\ProductsController::edit()]

[缺少 App\Http\Controllers\ProductsController::edit() 的参数 1]

Your route:

您的路线:

Route::get('productsedit', array('as'=> '/productsedit','uses'=>'ProductsController@edit'));

Will have to change to something like this:

将不得不更改为这样的:

Route::get('products/{$id}/edit', 'ProductsController@edit');

When calling the route in your view it will have to look something like this:

在您的视图中调用路由时,它必须如下所示:

'products/{{$product->id}}/edit'

Extra: You might want to take a look at Resource controllers since you are not really following restfull practices when it comes to your routes https://laravel.com/docs/5.4/controllers#resource-controllers

额外:你可能想看看资源控制器,因为你在路由方面并没有真正遵循 restfull 实践 https://laravel.com/docs/5.4/controllers#resource-controllers

回答by Datha Reico

your should give route id

你应该给路线ID

Route::get('productsedit/{id}', array('as'=> '/productsedit','uses'=>'ProductsController@edit'));

Route::get('productsedit/{id}', array('as'=> '/productsedit','uses'=>'ProductsController@edit'));

and in your function should like this

并且在你的函数中应该是这样的

`public function edit($id = null)
{
      //find the post in the db and sav it as a variable
    $product = Products:: findOrFail($id);

    //return view and pass in the var previously created 
    return view('/productsedit')->withProducts($product);
}`

hope this work !!!

希望这项工作!