控制器中的 Laravel null POST 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47077857/
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 null POST data in controller
提问by VividDreams
A few days ago I started to learn laravel on laracasts and I have a small problem.
几天前我开始在laracasts上学习laravel,遇到了一个小问题。
When I submit a form data using post method and want to access this data in my controller, I have an empty value.
当我使用 post 方法提交表单数据并希望在我的控制器中访问此数据时,我有一个空值。
Form:
形式:
<form method="POST" action="{{ url('/costs')}}">
{{ csrf_field() }}
<div class="form-group">
<label for="cost-title">Cost title</label>
<input type="text" class="form-control" id="cost-title" name="title" placeholder="Enter cost title">
</div>
<div class="form-group">
<label for="cost-price">Price</label>
<input type="text" class="form-control" id="cost-price" name="price" placeholder="Enter price">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
Controller:
控制器:
<?php
namespace App\Http\Controllers;
use App\Cost;
class CostsController extends Controller
{
public function index(){
$costs = Cost::all();
return view('costs.index', compact('costs'));
}
public function show(Cost $cost){
return view('costs.show', compact('cost'));
}
public function create(Cost $cost){
return view('costs.create');
}
public function store(){
dd(request()->all);
}
}
In my routes I'm triggering @store using post method:
在我的路线中,我使用 post 方法触发 @store:
Route::get('/costs', 'CostsController@index');
Route::get('/cost/{cost}', 'CostsController@show');
Route::get('/costs/create', 'CostsController@create');
Route::post('/costs', 'CostsController@store');
And when I try to dump and die the requests:
当我尝试转储并终止请求时:
public function store(){
dd(request()->all);
}
I have a null value. Can you please explain me why I can't see any data here?
我有一个空值。你能解释一下为什么我在这里看不到任何数据吗?
回答by Junaid Ahmad
Update your store method and pass a Request parameter like this:
更新您的 store 方法并传递一个 Request 参数,如下所示:
public function store (Request $request)
{
$input = $request->all();
dd($input);
}
Update:
更新:
The Illuminate\Http\Request
instance provides a variety of methods for examining the HTTP request for your application and extends the
实例提供了多种方法来检查您的应用程序的 HTTP 请求并扩展
Symfony\Component\HttpFoundation\Request
If your controller method is also expecting input from a route parameter you should list your route parameters after your other dependencies. For example, if your route is defined like so:
如果你的控制器方法也需要来自路由参数的输入,你应该在你的其他依赖项之后列出你的路由参数。例如,如果您的路线定义如下:
Route::put('user/{id}', 'UserController@update');
You may still type-hint the Illuminate\Http\Request and access your route parameter id by defining your controller method as follows:
你仍然可以通过定义你的控制器方法来输入 Illuminate\Http\Request 并访问你的路由参数 id,如下所示:
public function update(Request $request, $id)
{
//
}
So, in simple words for getting form parameters you have to pass a variable ($request) to your controller so that it can be accessible (lets just say that laravel store these form parameters in the $request variable, so you can easily pass it to controller or not). Hope it will clear up things :)
因此,简单来说,要获取表单参数,您必须将一个变量 ($request) 传递给您的控制器,以便它可以被访问(假设 Laravel 将这些表单参数存储在 $request 变量中,因此您可以轻松地传递它控制器与否)。希望它会澄清事情:)