Last active
March 14, 2023 12:01
-
-
Save ZiTAL/fd847d6064f98bcc008be3573b563d4c to your computer and use it in GitHub Desktop.
Laravel: Eloquent connect 2 tables using hasOneThrough
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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