Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Eloquent Join

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.

like image 846
Geartz Avatar asked Feb 19 '14 15:02

Geartz


People also ask

Can I use join in eloquent Laravel?

joinRelationship is a method introduced by the Eloquent Power Joins package. It works with any type of the existing Laravel relationships. The installation of the package is as simple as just running the following composer command, and you should already have access to everything that will be mentioned on this post.

What is polymorphic relationship in Laravel?

A one-to-one polymorphic relationship is a situation where one model can belong to more than one type of model but on only one association. A typical example of this is featured images on a post and an avatar for a user. The only thing that changes however is how we get the associated model by using morphOne instead.


2 Answers

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();
like image 77
alexrussell Avatar answered Oct 12 '22 16:10

alexrussell


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

like image 21
AbstractChaos Avatar answered Oct 12 '22 16:10

AbstractChaos