Laravel向控制器传递附加参数


Laravel Passing Additional Parameter To Controller

我需要通过点击按钮将一个额外的参数($uid)从我的index.blade.php传递到我的edit.blade.php。

我index.blade.php

:

<a href="{!!action('FlyersController@edit', ['id' => Auth::user()->id, 'uid' => 1])!!}" type="button" class="btn btn-block btn-lg btn-info vorlageBtn card-link">Edit </a>
我FlyersController

:

public function edit($id, $uid)
{
    return view('backend.flyers.edit')->withUid($uid);
}

使用上面的代码,我得到一个错误:"App'Http'Controllers'FlyersController::edit()缺少参数2"

我在这里做错了什么?

操作方法没有抛出错误。它来自那个URL的路由。

检查URL是否向控制器传递参数

如果这是您想要的URL localhost:8000/backend/flyers/10/edit?%24uid=1,那么第二个参数是在$request变量中,而不是在控制器函数参数中。

你应该传递一个数组到action() helper:

action('FlyersController@edit', ['id' => Auth::user()->id, 'uid' => 1])

我可以解决这个问题的唯一方法是在My FlyersController中使用以下代码:

public function edit(Request $request, $id)
{
    return view('backend.flyers.edit')->withRequest($request);
}

,然后访问uid与{{request->uid}}在我的视图。

如果有人有更好的解决方案,请告诉我。

使用此代码

return view('backend.flyers.edit', ['var1' => $var1, 'var2' => $var2]);

将两个或多个变量传递给视图