本文介绍了Laravel传递数据到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚开始学习laravel,想知道如何将与路由无关的数据传递给控制器。我想要完成的是创建一个能够嵌套项目的todo项目。
I just starting learning laravel and was wondering how to pass data unrelated to the route to a controller. What I'm trying to accomplish is create a todo item that is able to have nested items.
<a class="btn btn-success" href="{{route('lists.items.create',4)}}">Create New Item</a>
4只是一个硬编码的例子,看看它是否工作。
The 4 is just a hard-coded example to see if it was working.
public function create(TodoList $list, $item_id = null)
{
dd($item_id);
return view('items.create', compact('list'));
}
所以如果你创建一个项目, ,它将默认为 null
,否则设置它传递的任何内容。但我得到一个 NotFoundHttpException
。我将如何做到这一点。
So if your creating an item and don't pass in a parameter for id, it will default to null
otherwise set it to whatever was passed in. However I'm getting a NotFoundHttpException
. How would I be able to accomplish this.
任何帮助欢迎:)
推荐答案
路由,例如:
Route::get('create-item/{id}', [
'as' => 'lists.items.create',
'uses' => 'MyController@create'
])
现在,像下面这样调用路由:
Now, call the route like:
<a class="btn btn-success" href="{{route('lists.items.create', ['id' => 4])}}">Create New Item</a>
这篇关于Laravel传递数据到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!