【问题标题】:How to access the object using the collection of laravel?如何使用 laravel 的集合访问对象?
【发布时间】:2021-06-21 17:20:47
【问题描述】:

我有一个模块,我需要在数据库中插入所有每个数组对象的值,但是今天当我尝试转储对象时遇到问题,将对象放入数组中,它说尝试读取属性“客户”大批。目前我使用 laravel 我的后端

错误:

消息:“尝试读取数组上的属性“客户””,异常:“ErrorException

目标:获取每个对象。

转储请求全部:

    array:1 [
  "value" => array:5 [
    0 => array:8 [
      "key" => "0"
      "customer" => "1"
      "bank" => null
      "branch" => null
      "checkNo" => null
      "amount" => null
      "remarks" => null
      "checkDate" => null
    ]
    1 => array:8 [
      "key" => "1"
      "customer" => "2"
      "bank" => null
      "branch" => null
      "checkNo" => null
      "amount" => null
      "remarks" => null
      "checkDate" => null
    ]
    2 => array:8 [
      "key" => "2"
      "customer" => null
      "bank" => null
      "branch" => null
      "checkNo" => null
      "amount" => null
      "remarks" => null
      "checkDate" => null
    ]
    3 => array:8 [
      "key" => "3"
      "customer" => null
      "bank" => null
      "branch" => null
      "checkNo" => null
      "amount" => null
      "remarks" => null
      "checkDate" => null
    ]
    4 => array:8 [
      "key" => "4"
      "customer" => null
      "bank" => null
      "branch" => null
      "checkNo" => null
      "amount" => null
      "remarks" => null
      "checkDate" => null
    ]
  ]
]

收藏品 - dd($item):

 array:5 [
  0 => array:8 [
    "key" => "0"
    "customer" => "1"
    "bank" => null
    "branch" => null
    "checkNo" => null
    "amount" => null
    "remarks" => null
    "checkDate" => null
  ]
  1 => array:8 [
    "key" => "1"
    "customer" => "2"
    "bank" => null
    "branch" => null
    "checkNo" => null
    "amount" => null
    "remarks" => null
    "checkDate" => null
  ]
  2 => array:8 [
    "key" => "2"
    "customer" => null
    "bank" => null
    "branch" => null
    "checkNo" => null
    "amount" => null
    "remarks" => null
    "checkDate" => null
  ]
  3 => array:8 [
    "key" => "3"
    "customer" => null
    "bank" => null
    "branch" => null
    "checkNo" => null
    "amount" => null
    "remarks" => null
    "checkDate" => null
  ]
  4 => array:8 [
    "key" => "4"
    "customer" => null
    "bank" => null
    "branch" => null
    "checkNo" => null
    "amount" => null
    "remarks" => null
    "checkDate" => null
  ]
]

转储行的结果:

    Illuminate\Support\Collection {#203
  #items: array:5 [
    0 => array:8 [
      "key" => "0"
      "customer" => "1"
      "bank" => null
      "branch" => null
      "checkNo" => null
      "amount" => null
      "remarks" => null
      "checkDate" => null
    ]
    1 => array:8 [
      "key" => "1"
      "customer" => "2"
      "bank" => null
      "branch" => null
      "checkNo" => null
      "amount" => null
      "remarks" => null
      "checkDate" => null
    ]
    2 => array:8 [
      "key" => "2"
      "customer" => null
      "bank" => null
      "branch" => null
      "checkNo" => null
      "amount" => null
      "remarks" => null
      "checkDate" => null
    ]
    3 => array:8 [
      "key" => "3"
      "customer" => null
      "bank" => null
      "branch" => null
      "checkNo" => null
      "amount" => null
      "remarks" => null
      "checkDate" => null
    ]
    4 => array:8 [
      "key" => "4"
      "customer" => null
      "bank" => null
      "branch" => null
      "checkNo" => null
      "amount" => null
      "remarks" => null
      "checkDate" => null
    ]
  ]
}

转储:

$rows = collect($request->value);

    $collection = $rows->map(function ($item) {


        dd($item['customer']);
       
    });

我的请求负载:

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    问题是实际需要的数据在$request->value 中。正如错误所说,它的数组不是对象的集合。所以我认为应该是

    $rows = collect($request->value);
    
        $collection = $rows->map(function ($item) {
    
            
            dd($item['customer']);
           
    });
    

    或者你可以试试

      $collection = $rows->map(function ((object)$item) {
        
                
                dd($item->customer);
               
        });
    

    【讨论】:

    • 我已经这样做了,但错误显示未定义数组键“客户”
    • 你能给出实际的数组吗。所以我可以测试它以确保
    • 试试 dd($item) 你得到了什么
    • 如果我尝试使用 dd($item) 发布收藏项目。请查看新的更新内容 我插入了我的有效载荷的屏幕截图
    • 是 dd($request->all()) 或 dd($item) 的截图
    猜你喜欢
    • 1970-01-01
    • 2022-07-22
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-14
    • 1970-01-01
    相关资源
    最近更新 更多