【问题标题】:Get ID from while looping从while循环中获取ID
【发布时间】:2019-08-09 15:34:42
【问题描述】:

我想做一个员工请假申请,当员工提交请假申请时,他的老板会立即在电子邮件中收到通知

我在数据库中的表是这样的

id |    name     |      email           | person_in_charge |
1      Michael        michael.com                2
2      Johan          johan.com                  4
3      Lorem          lorem.com                  2
4      Ipsum          ipsum.com                  5
5      Dolor          dolor.com

我想,当用户 id 1 发送电子邮件时,收到电子邮件的人是用户 id 2、4 和 5,因为用户 id 2 是用户 id 1 的老板,用户 id 4 是老板用户 id 2,用户 id 5 都是老板。

我一直在使用while循环,但结果是收到电子邮件的人是用户ID 4和5,用户ID 2没有收到电子邮件

这是代码

$currentuserid = '1';

    $show= Users::where('id',$currentuserid)->first();

    if($show->pic_for!=null)
    {
        $pic_for = $tampil->pic_for;

        while($pic_for!=null)
        {
            $query = Users::where('id',$pic_for)->first();

            if($query->pic_for==null)
            {
                break;
            }else
            {
                $pic[]=$query->pic_for;
                $pic_for = $query->pic_for;

            }

        }
        dd($pic);

你知道我错过了什么吗?

谢谢

【问题讨论】:

    标签: laravel loops foreach while-loop


    【解决方案1】:

    当您进入循环时,您根本不保存第一个引用。第一个$pic_for 永远不会添加到数组$pic

    编辑/顺便说一句:这段代码基本上可以使用该循环对您自己的数据库进行 DDoS 攻击。只是一个想法,但您可以在它们之间创建 HABTM 关系,而不是遍历单个实例。所以你可以像$user->usersInCharge() 这样查询并遍历这个集合。

    【讨论】:

    • 嗨@everyman,感谢您之前的回答,但我仍然不明白您的编辑/顺便说一句评论。你能解释更多吗?谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    相关资源
    最近更新 更多