Php 雄辩的:在关系属性为0或关系不为'的情况下,约束渴望加载关系;不存在

Php 雄辩的:在关系属性为0或关系不为'的情况下,约束渴望加载关系;不存在,php,laravel,eloquent,Php,Laravel,Eloquent,我想获取所有报表模型,其中报表上传的状态属性等于0,或者报表上传关系不存在。Report和ReportUpload模型具有一对一的关系,ReportUpload属于Report 有些不确定如何使用eloquent的关系约束或任何其他方法进行此操作。任何帮助都将不胜感激 以下是我当前的代码: // initial query $reports = Report::whereHas('link', function($query) { $query->where('statu

我想获取所有
报表
模型,其中
报表上传
状态属性
等于0,或者
报表上传
关系不存在。
Report
ReportUpload
模型具有一对一的关系,
ReportUpload
属于
Report

有些不确定如何使用eloquent的关系约束或任何其他方法进行此操作。任何帮助都将不胜感激

以下是我当前的代码:

// initial query

$reports = Report::whereHas('link', function($query) {
        $query->where('status', 'complete');
    })->with('student', 'course', 'institution', 'reportUpload');


// apply constraint

if ($request->has('uploadStatus')) {
    $uploadStatus = $request->has('uploadStatus'); // 0 or 1

    if ($uploadStatus === 0) {
        $reports = $reports
            ->whereDoesntHave('reportUpload')
            ->orWhereHas('reportUpload', function($query) use ($uploadStatus) {
                $query->where('status', $uploadStatus);
            });
    } else {
        $reports = $reports->whereHas('reportUpload', function($query) use ($uploadStatus) {
            $query->where('status', $uploadStatus);
        });
    }
}
代码不会产生所需的结果

编辑

尝试此方法,但不确定是否正确:

$reports = $reports
    ->where(function ($query) use ($uploadStatus) {
        $query
            ->whereDoesntHave('reportUpload')
            ->orWhereHas('reportUpload', function($query) use ($uploadStatus) {
                $query->where('status', $uploadStatus);
            });
    });

尝试分离查询
whereDoesntHave
可能与
或wherehas
一起负计数,即使它是
语句:

$reportsNoUpload = $reports
            ->whereDoesntHave('reportUpload')->get();

$reportsIncomplete = $reports
            ->orWhereHas('reportUpload', function($query) use ($uploadStatus) {
                $query->where('status', $uploadStatus);
            })->get();

$reports = $reportsNoUpload->merge($reportsIncomplete);

首先,初始代码中有一些错误

1-您正在检查请求
是否具有上载状态。然后,
$uploadStatus==$request->has
将始终为
true

if ($request->has('uploadStatus')) {
    $uploadStatus = $request->has('uploadStatus');
所以我想你可能想要:

if ($request->has('uploadStatus')) {
    $uploadStatus = $request->input('uploadStatus');
2-您正在严格比较
$uploadStatus===0
,这可能不起作用,因为请求可能返回字符串
'0'
,而不是整数,因此您应该与
=
进行比较,或者将
$uploadStatus
强制转换为
(int)

在这之后,我认为您在问题中添加的代码按照预期工作:

$reports = $reports
->where(function ($query) use ($uploadStatus) {
    $query
        ->whereDoesntHave('reportUpload')
        ->orWhereHas('reportUpload', function($query) use ($uploadStatus) {
            $query->where('status', $uploadStatus);
        });
});

因为封装查询的
where
会将查询放在括号之间。

也许会有帮助?你建议我使用它吗?因为它已经在我的问题代码中了。如果你的意思是别的,请澄清,谢谢。对不起,@haakym,我误解了这个问题,我认为我之前的评论与此无关。很抱歉不用担心!感谢您的光临。Eloquent正是查询生成器-Eloquent在幕后使用查询生成器。只要
where(function($q){$q->where()->或where();})
-下面有一个答案,它显示了您的答案。我真不敢相信我错过了所有这些错误,这些错误会给你一个机会改正,然后再给你回复。一切都如我所愿!非常感谢您的帮助并感谢@jarek tkaczyk提供的查询提示。这种方法的问题是我不想执行
get()
,因为我有一个巨大的数据集,正在使用laravel datatables包对其进行分页。