Skip to content

Instantly share code, notes, and snippets.

@ZiTAL
Last active March 14, 2023 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZiTAL/fd847d6064f98bcc008be3573b563d4c to your computer and use it in GitHub Desktop.
Save ZiTAL/fd847d6064f98bcc008be3573b563d4c to your computer and use it in GitHub Desktop.
Laravel: Eloquent connect 2 tables using hasOneThrough
<?php
// We would like to get Destiny's table data, using Main's information and using the intermediate table Intermediate
App;
use Illuminate\Database\Eloquent\Model;
class Main extends Model
{
public function destiny()
{
return $this->hasOneThrough(
'App\Destiny', // 1. Destiny table
'App\Intermediate', // 2. Intermediate table
'Intermediate_primary_key', // 3. Intermediate's primary key
'Destiny_primary_key', // 4. Destiny's primary key
'Main_field', // 5. Main's field data
'Destiny_field'); // 6. Destiny's fiel data
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment