Laravel 将数据传递给控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30201692/
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
Laravel Passing Data to Controller
提问by Josip Dorvak
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.
我刚开始学习 laravel,想知道如何将与路由无关的数据传递给控制器。我想要完成的是创建一个能够包含嵌套项目的待办事项。
View
看法
<a class="btn btn-success" href="{{route('lists.items.create',4)}}">Create New Item</a>
The 4 is just a hard-coded example to see if it was working.
4 只是一个硬编码的例子,看看它是否有效。
Controller
控制器
public function create(TodoList $list, $item_id = null)
{
dd($item_id);
return view('items.create', compact('list'));
}
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.
因此,如果您创建一个项目并且不为 id 传递参数,它将默认以null
其他方式将其设置为传入的任何内容。但是我得到了一个NotFoundHttpException
. 我怎么能做到这一点。
Any help Welcome :)
任何帮助欢迎:)
回答by manix
You need to define the route, for example:
您需要定义路由,例如:
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>