如何解决“类型错误:传递给App \ Http \ Controllers \ Controller :: validate()的参数1必须是Illuminate \ Http \ Request的实例"? [英] How can I solve "Type error: Argument 1 passed to App\Http\Controllers\Controller::validate() must be an instance of Illuminate\Http\Request,"?

查看:685
本文介绍了如何解决“类型错误:传递给App \ Http \ Controllers \ Controller :: validate()的参数1必须是Illuminate \ Http \ Request的实例"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下:

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');
        ...
    }
}

echo '<pre>';print_r($request->get('data'));echo '</pre>';die();的结果如下:

{数据":{"id":46,数量":1,总计":100000,信息":切尔西",名称":危险","request_date":"14- 08-2017 16:26:00},"已过期:" 2017-08-14T06:27:00.738Z}

{"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 :

类型错误:参数1传递给 App \ Http \ Controllers \ Controller :: validate()必须是的实例 照亮\ Http \ Request,...

Type error: Argument 1 passed to App\Http\Controllers\Controller::validate() must be an instance of Illuminate\Http\Request,...

如何解决该错误?

推荐答案

传递给validate的第一个参数应该是Illuminate\Http\Request的实例.

The first parameter passed to validate should be an instance of Illuminate\Http\Request.

因此,您需要将$request对象作为第一个参数传递给validate方法

So you need to pass the $request object to validate method as the first parameter like this

$this->validate($request, [
    'validation' => 'rules',
]);

如果您以JSON形式接收输入,请确保将请求的Content-Type标头设置为application/json

If you are receiving input as JSON make sure Content-Type header of the request is set to application/json

jQuery ajax示例

jQuery ajax example

$.ajax({
    url: url,
    type: 'POST',
    data: {
        'id': 'value', 
        'quantity': 'value'
    },
    dataType: 'json',
    contentType: 'application/json'
});

这篇关于如何解决“类型错误:传递给App \ Http \ Controllers \ Controller :: validate()的参数1必须是Illuminate \ Http \ Request的实例"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆