My code like this :
public function functionA(Request $request)
{
...
if($request->get('data')) {
//echo '<pre>';print_r($request->get('data'));echo '</pre>';die();
$data = json_decode($request->get('data'), true);
$data = collect($data['data']);
$this->validate($data, [
'id'=>'required|numeric',
'quantity'=>'required|numeric',
]);
$input = $data->only('id', 'quantity','request_date');
...
}
}
The result of echo '<pre>';print_r($request->get('data'));echo '</pre>';die();
like this :
{"data":{"id":46,"quantity":1,"total":100000,"information":"chelsea","name":"Hazard","request_date":"14-08-2017 16:26:00"},"expired":"2017-08-14T06:27:00.738Z"}
If the code executed, there exist error like this :
Type error: Argument 1 passed to App\Http\Controllers\Controller::validate() must be an instance of Illuminate\Http\Request,...
How can I solve the error?
答案 0 :(得分:1)
传递给validate
的第一个参数应该是Illuminate\Http\Request
的实例。
所以你需要将$request
对象作为第一个参数传递给validate
方法
$this->validate($request, [
'validation' => 'rules',
]);
如果您以JSON身份接收输入,请确保请求的Content-Type
标头设置为application/json
jQuery ajax示例
$.ajax({
url: url,
type: 'POST',
data: {
'id': 'value',
'quantity': 'value'
},
dataType: 'json',
contentType: 'application/json'
});
答案 1 :(得分:0)
如果您在控制器中使用use Request;
,请将其替换为
use Illuminate\Http\Request;