Laravel Eloquent Join
I left a comment earlier about this but now I'm pretty sure it's the answer you're looking for: you should use belongsToMany
rather than hasManyThrough
. So first, might I suggest you rename your tables and models to follow Laravel's conventions (plural snake_case table names, singular snake_case alphabetical order pivot table names, singular StudlyCaps model names), that way you'll have the following situation:
Tables:
- games
- id
- name
- game_option
- id
- game_id
- option_id
- options
- id
- option
- name
Now you can rewrite your models to conform to the new structure, and use a belongsToMany relationship too:
class Game extends Eloquent{ public function platforms() { return $this->belongsToMany('Option'); }}class Option extends Eloquent{ public function platforms() { return $this->belongsToMany('Game'); }}
Note: you don't have to model the pivot table (game_option
) unless you store extra data on the pivot.
Now you should be good to get all options for a given game:
$options = Game::find(1)->options;
Or if you need to get all platforms (though I am trying to infer meaning of your code here regarding options and platforms):
$platforms = Game::find(1)->options()->whereOption('platform')->get();
you can use the with method with eloquent
$game = Game::where('id',1)->with('platforms')->get();
Should return you the game and platforms
For documentation I would first start with the documentation provided (find it to be about 50% complete) and with the api everything else is covered
You would have to model your tables like:
**games**idname**game_options**idgame_idname**game_platform**idgame_options_idplatform_id /* which i assume you get from a platform master table */
Then in your Game Class:
class Game extends Eloquent{ protected $table = 'games'; public function platforms() { return $this->hasManyThrough('GamePlatform','GameOptions','game_id','game_options_id'); }}
Now, this would be assuming that Game Platform belongs to Games through Game Options.