1

I have the following set for relations in my eloquent models/db:

Student has many PhdReport

PhdReport has one Link

I need to get Students where the (single) most recent PhdReport (by the attribute date_to) is more than 6 months and the Link connected to the PhdReport has a status of 'complete'.

I am trying to do this using eloquent relationships and I'm pretty new to the concept of querying relationships so I would like to know if there's a better approach to the one I'm taking.

Here's the relevant code so far:

PhdReport.php

public function link()
{
    return $this->belongsTo(\App\Models\Link::class);
}

Student.php

public function phdReport()
{
    return $this->hasMany(\App\Models\PhdReport::class);
}

public function latestPhdReport()
{
    return $this->hasOne(\App\Models\PhdReport::class)->latest('date_to');
}

/* this doesn't work! */    
public function lastPhdReportSixMonthsAgo()
{
    $sixMonthsAgo = \Carbon\Carbon::now()->subMonth(6);

    return $this->whereHas('latestPhdReport', function ($query) use ($sixMonthsAgo) {
        $query->where('date_to', '<=', $sixMonthsAgo);
    });
}

This is my best shot at it so far but I am unsure if the first whereHas applies to the second whereHas also?

$sixMonthsAgo = \Carbon\Carbon::now()->subMonth(6);

$students = $this->student
    ->whereHas('phdReport.link', function ($query) {
        $query->where('status', 'complete');
    })
    ->whereHas('latestPhdReport', function ($query) use ($sixMonthsAgo) {
        $query->where('date_to', '<=', $sixMonthsAgo);
    })
    ->get();

If I run:

$students = $this->student
    ->has('lastPhdReportSixMonthsAgo')
    ->get();

I get:

BadMethodCallException in Builder.php line 1994:
Call to undefined method Illuminate\Database\Query\Builder::getRelated()

Any advice would be much appreciated!

3
  • This will do softonsofa.com/… Dec 2, 2015 at 13:15
  • @JarekTkaczyk thanks for the link. I have a working solution now but it definitely needs some fine tuning. I will post up an answer once I'm happy with it. Thanks again for your help - really appreciate it.
    – haakym
    Dec 3, 2015 at 9:05
  • 1
    Cool. Share your code once you're done. Dec 3, 2015 at 14:06

1 Answer 1

0

This is my current solution, I haven't had time to refactor yet but I'm sure it can be improved by doing only a query and no filter. Will edit when I get time but thought I'd put it up as it might help someone else. Much credit to @JarekTkaczyk

$monthsAgo = \Carbon\Carbon::now()->subMonth($months);

// Query 1: students with a completed report
$studentsWithCompletedLink = $studentsWithCompletedLink
    ->whereHas('phdReport.link', function ($query) {
        $query->where('status', 'complete');
    })
    ->get();

if ($months != 0) {
    // filter through to get students where the report date_to is more than $months months ago
    $studentsWithReport = $studentsWithCompletedLink->filter(function ($student) use ($monthsAgo) {
        return $student->latestReport->date_to <= $monthsAgo;
    });
}

Student.php

public function latestPhdReport()
{
    return $this->hasOne(\App\Models\PhdReport::class)->latest('date_to');
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.