laravel pass value from query to with() function

  laravel, laravel-7

here is my quary

    $fix = Fixture::select('fixtures.*')
        ->whereDate('kickoff', '<', $date)
        ->with('competition')
        ->with('teamA')
        ->with(['teamA.standing' => function ($q) {
            $q->whereColumn('standings.competition_id', '=', {{here i want the competition_id from competitions or fixtures table}} );
        }])
        ->with('teamB')
        ->with(['teamB.standing' => function ($q) {
            $q->whereColumn('standings.competition_id', '=',  {{here i want the competition_id from competitions table or fixtures table}});
        }])->get();

code from Fixture model

 public function teamA()
    {
        return $this->belongsTo(Team::class,'team_a','id');;
    }
    public function teamB()
    {
        return $this->belongsTo(Team::class,'team_b','id');;
    }

Code from Team Model

   public function standing()
    {
        return $this->hasOne(Standing::class);;
    }

code from Standing model

   public function team(){
        return $this->belongsTo(Team::class);
    }

    public function competition()
    {
        return $this->belongsTo(Competition::class);
    }

can someone help me to get this done or if i am doing it wrong whats the correct way

->with([‘teamA.standing’ => function ($q) {
$q->whereColumn(‘standings.competition_id’, ‘=’, {{here i want the competition_id from competition table or fixtures table}} );
}])

Source: Laravel

Leave a Reply