1

I got 3 tables. Table 1 & 2 has their ids as foreign keys in third one(pivot).

Relations for first one is

$this->hasMany("App\Pivot","game_id");

, second is

$this->belongsToMany("App\Pivot","army_id");

and pivot has relationships with both of them i.e belongsTo.

My schema: schema

I tried accessing it in controller of first one like this:

$games= Game::with("armies")->get();

armies

Result that i get is array of games where instead of individual army data , i get collection from pivot table.

I can loop through it and get it that way, is there more elegant way of doing it?

2 Answers 2

3

If you are using pivot table this is the way how to do it.

Games Model

public function armies()
{
    return $this->belongsToMany(App\Armies::class, 'pivot_table', 'game_id', 'army_id');
}

Armies Model

public function armies()
{
    return $this->belongsToMany(App\Games::class, 'pivot_table', 'army_id', 'game_id');
}

Access the relationship like this..

Controller

App\Games::first()->armies()->get();
or
App\Games::first()->armies
or
App\Games::find(1)->armies
Sign up to request clarification or add additional context in comments.

2 Comments

It works. I am using $games = Game::with("armies")->get();. I just want to ask you couple of follow ups. In array of armies, singular object has property pivots. Is it byproduct of relationship or is it removable? Second: If i remove 'army_id' from first one you are written, it shows exactly the same data as with it. Why is that?
1

If you're going to use an intermediate table like that I'd probably do something like this:

Games model

    public function armies()
    {
        return $this->belongsToMany('App\Armies');
    }

Armies model

    public function games()
    {
        return $this->belongsToMany('App\Games');
    }

I'd keep the table structures all the same but rename the "pivot" table to armies_games since this is what Laravel will look for by default. If you want to keep it named Pivots, you'll need to pass it in as the second argument in belongsToMany.

With this, you don't really need the Pivot model, you should just be able to do:

$armies = Game::first()->armies()->get();

or

$armies = Game::find(3)->armies()->orderBy('name')->get();

or

$game = Game::first();
foreach ($game->armies as $army) {
  //
}

etc.

3 Comments

Not really applicable. I don't "know" in advance id. Plus it for all 'games' respectively. I said i'm trying avoiding loops and searching more "laravel way" of doing it.
The answer posted 10 minutes after mine explains exactly what I explained almost verbatim. I'm curious as why mine is "not applicable" but the other was accepted?
Because my pivot table is called "pivot", not standard Laravel naming i.e. "army_game". This is my fault. Didn't know about pivot table naming convection. Your would work if it were standard named.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.