1

I read this post which helped, but need more guidance please.

I need a set of Results for a particular $batteryid (on Result) and a particular $age (on Test model) and a particular $gender (from Athlete). Each Test belongs to one athlete. Each test has many results.

Model Battery:

class Battery extends Model{
    public function results(){
        return $this->hasMany('App\Result');
    }    
}

Model Result:

class Result extends Model{
    public function test(){
        return $this->belongsTo('App\Test', 'test_id');
    }

    public function battery(){
        return $this->belongsTo('App\Battery', 'battery_id');
    }    
}

Model Test:

class Test extends Model{
    public function athlete(){
        return $this->belongsTo('App\Athlete', 'athlete_id');
    }

    public function results(){
        return $this->hasMany('App\Result');
    }
}

I get the correct results with the following, but it's two queries:

$tests = Test::whereHas('Athlete', function($query) use($gender){
                $query->wheregender($gender);
           })->where('age', $age)->get();

$test_ids = $tests->pluck('id');
$results = Result::where('battery_id', '=', $battery)
          ->whereIn('test_id', $test_ids)
          ->get();

I'd like to use model approach, but am completely stuck. test.age not recognised and not sure how to get the age from athlete model. My attempt:

            $results = Result::with('test')
            ->where('test.age', '=', $age)
            ->join('batteries', 'batteries.id', '=', 'test.battery_id')
            //->where('test->athlete().gender', '=' $gender)
            ->where('battery_id', '=', $battery)
            ->get(); 

1 Answer 1

1

You can use whereHas for the condition and with method for eager loading it.

    $results = Result::whereHas('test', function($q) use ($age) {
          $q->where('age', '=', $age);
          $q->whereHas('athlete', function ($q) {
              $q->where('gender', 'male');
          });

        })
        ->with('test')
        ->whereHas('battery', function($q) use($battery) {
            $q->where('battery_id', '=', $battery);
        })
        ->with('battery')
        ->get();
Sign up to request clarification or add additional context in comments.

11 Comments

It worked before you added the gender bit. I'm testing different syntax now, perhaps the 2nd $q-> must come out?
Does your athlete table got gender column?
If you use gender variable, you need to include it in function closure.
yes, I added use($gender). Also tried the gender hard coded exactly as your code. Getting parse error for unexpected }. I've moved things around, not working yet. Works 100% before gender validation.
I think I fixed it @louisav
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.