Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在laravel控制器中获取json_Php_Laravel - Fatal编程技术网

Php 在laravel控制器中获取json

Php 在laravel控制器中获取json,php,laravel,Php,Laravel,我正在通过ajax向controller发送json数据,但我无法最终保存我的数据 密码 我在playload中发送的数据 [{specification_id: "6", text_dec: "1", product_id: "21"},…] 0: {specification_id: "6", text_dec: "1", product_id: "21"} 1: {specification_id: "7", text_dec: "3", product_id: "21"} 2: {spe

我正在通过ajax向controller发送json数据,但我无法最终保存我的数据

密码
我在playload中发送的数据

[{specification_id: "6", text_dec: "1", product_id: "21"},…]
0: {specification_id: "6", text_dec: "1", product_id: "21"}
1: {specification_id: "7", text_dec: "3", product_id: "21"}
2: {specification_id: "31", longtext_dec: "fsg", product_id: "21"}
控制器

public function addnewcustomsubspecifications(Request $reqss)
    {
        // dd($reqss->json()->all());

    //   $this->validate($reqss, array(
    //     'product_id' => 'required',
    //     'specification_id' => 'required',
    //     'text_dec' => 'nullable',
    //     'longtext_dec' => 'nullable',
    //   ));

      $datas = $reqss->json()->all();
      foreach($datas as $data){
          $add = CustomProductSpecification::create([
              'product_id' => $data['product_id'],
              'specification_id' => $data['specification_id'],
              'text_dec' => $data['text_dec'],
              'longtext_dec' => $data['longtext_dec'],
          ]);
          $parent = Specification::where('id', '=', $data['specification_id'])->first();
      }

      return response()->json(array('data'=>$add,'parent'=>$parent));
    }
如果我评论我的验证,我将得到:

"message": "Undefined index: longtext_dec",
    "exception": "ErrorException",
如果没有,我得到:

{"message":"The given data was invalid.","errors":{"product_id":["The product id field is required."],"specification_id":["The specification id field is required."]}}
我的数据是这样的:

array:3 [
  0 => array:3 [
    "specification_id" => "6"
    "text_dec" => "1"
    "product_id" => "21"
  ]
  1 => array:3 [
    "specification_id" => "7"
    "text_dec" => "3"
    "product_id" => "21"
  ]
  2 => array:3 [
    "specification_id" => "31"
    "longtext_dec" => "fsggf"
    "product_id" => "21"
  ]
注意:我认为验证问题是因为
$this->validate($reqss,array(
我需要使用类似
$this->validate($reqss->json(),数组(
),但这不可能 方式

思想
  • 我认为我的验证
    $reqss
    应该更改为验证json代码(如上所述)
  • 如果由于任何原因无法使用验证,我想我需要忽略我的
    longtext_dec
    text_dec
    部分的if语句,以防它们没有提供,并且不返回上面的第一个错误
  • PS:我的想法对你来说可能很傻,但如果我知道真实答案,我会 不会在这里问吧?:)


    好的,有什么想法吗?

    您可能需要单独计算每个数组。我想不出任何可以同时验证多行的方法。但是,由于text_dec和longtext_dec可以为空或缺失,因此在获取值时需要考虑这一点:

    $add = CustomProductSpecification::create([
        'product_id' => $data['product_id'],
        'specification_id' => $data['specification_id'],
        'text_dec' => array_key_exists('text_dec', $data) ? $data['text_dec'] : null,
        'longtext_dec' => array_key_exists('longtext_dec', $data) ? $data['longtext_dec'] : null,
    ]);
    
    三元数将确保数组具有该列。如果是,它将添加该值,如果不是,它将传入一个空值