Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AdhamLap's avatar

db query to relationship

my query

$data = DB::table('countries')
        ->leftJoin('matches', function ($join) {
            $join->on('matches.hometeam', '=', 'countries.id')
            ->orOn('matches.awayteam', '=' ,'countries.id');
        })
        ->where('countries.id', $id)->get();

        return dd($data);

how i can get same results use relationship

0 likes
4 replies
cmdobueno's avatar

The best way? Use a relationship and a model.

Otherwise you are just doing all the work that has already been done...

1 like
cmdobueno's avatar

First, a slight modification to your code:

model

public function awayteam(){
    return $this->hasOne("App\WorldCup"); //fill in proper keys
}

public function hometeam(){
    return $this->hasOne("App\WorldCup); //fill in proper keys
}
AdhamLap's avatar

query to get many rows from table matches

Please or to participate in this conversation.