Skip to content

Instantly share code, notes, and snippets.

@vidux
Created March 28, 2021 15:11
Show Gist options
  • Save vidux/87c734b0f6fabbb5a838a8d25074c1d7 to your computer and use it in GitHub Desktop.
Save vidux/87c734b0f6fabbb5a838a8d25074c1d7 to your computer and use it in GitHub Desktop.
Laravel BelongsToOneThrough like relationship
<?php
//Model story
/*
`Spreedsheet` have many `Rows` `Row` have many `RowValues`
`Spreedsheet` parent of `Rows`, `Rows` parent of `RowValues`
*/
//table structure
/*
`Spreedsheet`
------------
|id | name |
-----------
`Rows`
----------------------
|id | spreedsheet_id |
---------------------
`RowValues`
----------------------
|id | row_id | value |
----------------------
*/
//get `Spreedsheet` from RowValues Table
class RowValues extends Model
{
public function spreedsheet()
{
return $this->hasOneThrough(Spreedsheet::class,Rows::class,'id','id', 'row_id' ,'spreedsheet_id') ;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment