将值传递到Laravel中的路由

3

我在Stack Overflow上查看了很多问题,但没有找到答案,所以在这里发布。

我在我的视图中有一个像这样的按钮

 <a class = "btn btn-primary btn-flat" 
href = "{{ route('WaterTanks.create') }}"> Create New Tank</a>

我的网页路由如下

Route::resource('WaterTanks', 'WaterTankController');

我的控制器方法如下所示

public function create() {
        return view('WaterTanks.create');
    }

我希望你能够在路由中传递变量,可能像这样:

我想传递变量到该路由中


<a class = "btn btn-primary btn-flat"  
href = "{{ route('WaterTanks.create')->with('type','$type') }}"> 
Create New Tank</a>

并且在我的控制器方法中像这样获取该变量

public function create($type) {
        return view('WaterTanks.create')->with('type',$type);
    }

但这并没有发生。出现了以下错误:
Call to a member function with() on string

那么我应该如何做到呢?

你所需要做的就是 {{ route('WaterTanks.create', '$type') }}。 - usrNotFound
1个回答

3
您可以像这样发送参数。
{{ route('WaterTanks.create', ['type' => $type]) }}

但是使用 Route::resource()create() 中不需要参数。因此,您必须手动定义该路由。

Route::get('WaterTanks/create/{type}', 'WaterTankController@create')->name('WaterTanks.create');

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接