如何检查一个表字段是null是否从其他表字段中选择数据并订购它

发布于 2025-02-04 06:36:23 字数 912 浏览 2 评论 0原文

我有两个表1(出勤)和2(出席率_corthe),我正在询问此问题以使用时间字段以ASC顺序出勤:

foreach ($employee_calendars as $key => $employee_calendar) {
        $employee_calendar->attendance = Attendance::where("employee_id", $employee->id)
            ->whereDate("date", "=", $employee_calendar->date)
            ->orderBy('time', 'asc')
            ->with("requests")
            ->get();
    }

问题是,当我出勤表中的时间字段无效时,将这些数据放在响应中, NULL的情况我想检查此出席的出勤校正是否有任何出席检查correct_time字段在出席_correction中而不是出勤时间字段,以下是我的表图,我的表图是带有突出显示的行,我也有出勤表(请求)的关系

public function requests()
{
    return $this->hasMany("App\Models\AttendanceCorrection", "attendance_id");
}

。 a href =“ https://i.sstatic.net/604ku.png” rel =“ nofollow noreferrer”> atcationance_correction table

出勤表

I have two tables 1(Attendance) and 2(Attendance_correction) I am querying this to get attendance in asc order using time field:

foreach ($employee_calendars as $key => $employee_calendar) {
        $employee_calendar->attendance = Attendance::where("employee_id", $employee->id)
            ->whereDate("date", "=", $employee_calendar->date)
            ->orderBy('time', 'asc')
            ->with("requests")
            ->get();
    }

problem is that when time field in my attendance table is null it puts this data on top of response, in the case of null I want to check if there is any attendance correction of this attendance check correct_time field in attendance_correction instead of attendance time field following are my table pictures with highlighted rows, I also have relation in attendance table (requests) for attendance_correction table:

public function requests()
{
    return $this->hasMany("App\Models\AttendanceCorrection", "attendance_id");
}

attendance_correction Table

attendance table

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿忘初心 2025-02-11 06:36:23

您可以做这样的事情:

Attendance::select(['*', \DB::raw('IF(`time` IS NOT NULL, `time`, `attendance_correction.correct_time`) `correctTime`')])
->join->on('attendance_corrrection.attendence_id', '=', 'attendance.id');
        ->where("employee_id", $employee->id)
        ->whereDate("date", "=", $employee_calendar->date)
        ->orderBy('correctTime', 'asc')
        ->get(); 

You can do something like this:

Attendance::select(['*', \DB::raw('IF(`time` IS NOT NULL, `time`, `attendance_correction.correct_time`) `correctTime`')])
->join->on('attendance_corrrection.attendence_id', '=', 'attendance.id');
        ->where("employee_id", $employee->id)
        ->whereDate("date", "=", $employee_calendar->date)
        ->orderBy('correctTime', 'asc')
        ->get(); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文