【问题标题】:Laravel Controller: "for" loop getting value of first iteration onlyLaravel 控制器:“for”循环仅获取第一次迭代的值
【发布时间】:2022-01-11 20:46:52
【问题描述】:

很抱歉提出这个问题,但我似乎无法找到解决问题的方法。我有以下代码:

for($x=0; $x<=2; $x++){
            $users = User::where('division', 1)
            ->where('has_been_assigned', null)
            ->first();

            $assignedLead = NewLeads::where('pic_endorsed_to', null)
            ->first();

            $assignedLead->pic_endorsed_to = $users->id;
            $assignedLead->save();

        }

所以在我的 MySQL NewLeads 表中,我希望实现这一点(将潜在客户分配给用户,然后将下一个潜在客户分配给下一个用户,依此类推):

id     pic_endorsed_to (FKEY)
 1                         1
 2                         2
 3                         3

相反,我得到了这个:

 id     pic_endorsed_to (FKEY)
  1                         1
  2                         1
  3                         1

谁能告诉我如何达到预期的效果?

非常感谢!

【问题讨论】:

  • 循环有什么用?在 $assignedLead 和 $users 查询中,您正在获取一条相同的记录?
  • 您好,$assignedLead 获取 Leads(或者您可以说“Tasks”)集合,而 $users 访问 User 集合以获取要分配给“lead”或“task”的用户。
  • 哦,你问的是循环的使用。我的目标是获取与设置的迭代次数一样多的一条记录。
  • 您没有更新用户。所以在每次迭代中都有相同的用户。如果没有用户可以分配怎么办??
  • 您好,我可以为此创建一个验证。但我仍在为用户分配线索/任务的过程中。但是正如你所看到的,我在分配一个又一个用户时遇到了麻烦,因为 for 循环只获取第一个用户的值,而不是接下来的两个。

标签: php laravel


【解决方案1】:

为简单起见,使用 OP 提供的代码(在我写作时):

   $users = User::where('division', 1)
        ->where('has_been_assigned', null)
        ->get();
   foreach ($users as $user){
    $assignedLead = NewLeads::where('pic_endorsed_to', null)
        ->first();
    if(assignedLead){
        $assignedLead->pic_endorsed_to = $users->id;
        $assignedLead->save();
    }else{ break; }
   }

我在这里所做的只是获取用户并将这些用户的 id 分配给值为 null 的 NewLeads。

在每个循环中,它将获取第一行 pic_endorsed_to null 并将值更新为用户的 id。

【讨论】:

    【解决方案2】:

    这是因为您没有在循环内的任何地方使用 $x 来使其动态化。 根据要求在某处使用 $x 或先删除。

    【讨论】:

      【解决方案3】:

      您每次都在运行 -&gt;first();,而 where 子句没有任何动态条件,因此它只会一次又一次地获取 first 记录。

      【讨论】:

        【解决方案4】:

        首先将-&gt;first() 更改为-&gt;get()

        然后检查这一行,并使用下面给出的代码进行更改:

        where('pic_endorsed_to', null)
                    ->first();
        

        应该是这样的

        where('pic_endorsed_to', $mydetail)
                    ->get();
        

        【讨论】:

          【解决方案5】:

          感谢您的意见。好像 zahid 的评论解决了我目前的问题:

          您没有更新用户。所以在每次迭代中都有相同的用户。如果没有用户可以分配怎么办? – 扎希德·哈桑·埃蒙

          只需使用“已分配”标记/更新用户的状态。

          谢谢大家!

          【讨论】:

            【解决方案6】:

            你可以这样做。

            解决方案 1:

            return collect($models->items())->each(function ($item, $index){
                        if( $item->quoteReturnDetail){  echo  $item ;  exit(); }   });
            

            解决方案 2

               return collect($models->items())->each(function ($item, $index){
                        if( index === 2 ){  echo  $item ;  exit(); }   });
            

            【讨论】:

              猜你喜欢
              • 2010-12-28
              • 1970-01-01
              • 2019-06-05
              • 2014-12-16
              • 1970-01-01
              • 1970-01-01
              • 2019-12-19
              • 1970-01-01
              • 2018-07-07
              相关资源
              最近更新 更多