代码之家  ›  专栏  ›  技术社区  ›  Forrest

无法通过属性laravel 5.8访问集合

  •  1
  • Forrest  · 技术社区  · 5 年前

    因此,我在请求中有三个相同长度的不同数组,下面是我如何将它们组合到一个集合中:

    $inputs = collect();
    $keys = collect(['id', 'username', 'email']);
    foreach ($request['ids'] as $index => $id) {
       $username = $request['usernames'][$index];
       $email = $request['emails'][$index];
       $inputs->push($keys->combine([$id, $username, $email]));
    }
    

    我看结果是正确的:

    enter image description here

    但是,当我遍历集合时,无法访问它:

    foreach ($inputs as $input) {
        dd($input->id); // Property [id] does not exist on this collection instance.
    }
    

    这是dd($input)的结果:

    enter image description here

    对这个问题有什么看法吗?(另一种将数组组合成一个集合的简单方法也将受到欢迎!)

    谢谢您。

    2 回复  |  直到 5 年前
        1
  •  2
  •   Majid Alaeinia georak    5 年前

    这是一个集合,你应该这样得到它: dd($input['id']) .

        2
  •  0
  •   mohamed hassan    5 年前

    可以合并数组bt array_merge

    array_merge($a1,$a2)
    

    或收集

    $c=collect([$arr1,$arr2])
    

    如果你愿意就摘

    $c->pluck('username')