Chciałbym uzyskać wszystkie modele Report
gdzie relacja ReportUpload
jest własnością status
równa 0 lub gdzie ReportUpload
relacja nie robi " t istnieje. Modele Report
i ReportUpload
mają relację jeden do jednego, ReportUpload
należy do Report
.Eloquent: Ograniczanie chętny relację obciążenia gdzie nieruchomość relacja wynosi 0 lub związek nie istnieje
Nie jest pewne, jak to zrobić, stosując elokwentne ograniczenia zależności lub dowolną inną metodę. Każda pomoc będzie doceniona.
Oto mój bieżący kod:
// 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);
});
}
}
Kod nie przynieść oczekiwanych rezultatów.
Edit
Trying to podejście, ale nie wiem, czy to jest poprawne:
$reports = $reports
->where(function ($query) use ($uploadStatus) {
$query
->whereDoesntHave('reportUpload')
->orWhereHas('reportUpload', function($query) use ($uploadStatus) {
$query->where('status', $uploadStatus);
});
});
Być może [whereDoesnHave] (https://laravel.com/api/5.1/Illuminate/Database/Eloquent/Builder.html#method_whereDoesntHave) może pomóc? –
Sugerujesz, że go używam? Ponieważ jest już w moim kodzie w pytaniu. Jeśli chodzi ci o coś innego, wyjaśnij, dziękuję. – haakym
Przepraszam, @haakym, źle zrozumiałem pytanie, nie sądzę, że mój poprzedni komentarz jest istotny. Przepraszam! –