9

I'm fairly new with Laravel. I'm still trying to learn it. My question is:

I have 3 tables named

  • games
  • game_options
  • game_platforms

    enter image description here

I have 3 Models for those tables

  • Game Model

    class Game extends Eloquent
    {
        protected $table = 'games';
    
        public function platforms()
        {
            return $this->hasManyThrough('GamePlatform','GameOptions','id','game_id');
        }
    }
    
  • GamePlatform Model

    class GamePlatform extends Eloquent
    {
        protected $table = 'game_platform';
    }
    
  • GameOption Model

    class GameOptions extends Eloquent
    {
        protected $table = 'game_options';
    }
    

So when I do

$game = Game::find(1)->platforms;

It only shows,

{"id":1,"platform_id":20,"game_id":1}
{"id":1,"platform_id":21,"game_id":1}
{"id":1,"platform_id":22,"game_id":1}
{"id":1,"platform_id":23,"game_id":1}
{"id":1,"platform_id":24,"game_id":1}

But I need game name and platform names with those ID's. The thing is, I want to do this with eloquent only. I could go with "DB" or oldschool SQL but I want to learn if this way is possible or not.

Also I'm looking for better documentation/books for laravel. Most of what I read were only introduce laravel or far too advanced for me.

1
  • I might be way off base here, but I feel that you're using hasManyThrough when belongsToMany would fit here. I don't know much about hasManyThrough so you can certainly tell me if I'm wrong, but maybe try changing that. At least then calling $game->platforms() will give you a Collection of GameOptions not GamePlatforms Feb 19, 2014 at 16:05

3 Answers 3

8

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();
2
  • As I mention above, I've kind of had to infer the meaning in your data, so do let me know if your platform/option stuff doesn't work the way I have assumed and I'll have a go at updating the examples. Feb 19, 2014 at 16:24
  • great explaination of pivot tables!
    – toesslab
    Apr 28, 2015 at 20:17
3

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

0
1

You would have to model your tables like:

**games**
id
name

**game_options**
id
game_id
name

**game_platform**
id
game_options_id
platform_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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.