【问题标题】:Can't create new array foreach record in index collection laravel无法在索引集合 laravel 中创建新的数组 foreach 记录
【发布时间】:2018-11-08 20:24:34
【问题描述】:

我有一个客户索引,其中显示了他们的购买摘要。

如果他们的任何购买未被同意,我需要在接受栏中打叉。为此,我将为每个客户创建一个接受数组,并使用 in_array() 来检查 null

但是我无法为每个客户创建一个数组,它只保留一个接受数组并从两个客户那里添加值。

客户控制器(索引)

$customers = Customer::with('paymentplans')->orderBy('created_at', 'desc')->get();

客户索引

foreach($customers as $customer){
        foreach($customer->paymentplan as $plan){
             $acceptance[] = $plan->accepted;
             }
}

这将输出以下数组:

array:7 [▼
0 => null
1 => 1
2 => 1
3 => 1
4 => 1
5 => 1
6 => 1
]  

我的两个客户需要这样的东西

array:5 [▼
  0 => null
  1 => 1
  2 => 1
  3 => 1
  4 => 1
  ]
array:2 [▼
  0 => 1
  1 => 1
]

【问题讨论】:

    标签: arrays laravel foreach


    【解决方案1】:

    foreach($customers 作为 $customer){

    foreach($customer->paymentplan as $plan){
         $acceptance[$customer->id][] = $plan->accepted;
    }
    

    }

    【讨论】:

      【解决方案2】:
      foreach($customers as $customer){
          foreach($customer->paymentplan as $plan){
               $acceptance[$customer->id] = $plan->accepted;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-12-13
        • 1970-01-01
        • 2018-06-27
        • 1970-01-01
        • 2021-12-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多