shawnest
shawnest

Reputation: 1429

Fetch multiple tables through hasManyThrough Laravel Eloquent

Database

I'm kind of new to databases and I made this small database, but I have problems fetching data from it. Im trying to get all the racers from the logged in user, and it works properly, but if I enter $pigeons = $user->racer I only get back the racer table. I would like to know the attributes of the racers from the pigeons table aswell. I've made it work with query builder left joining the tables but I'm not sure why I set up this relationship if I can't use Laravel inner method.

In the User model I have these relationships:

public function pigeons(){
        return $this->hasMany('App\Pigeon');
    }

    public function racers(){
        return $this->hasManyThrough('App\Racer', 'App\Pigeon');
    }

This is the Pigeon model:

public function user(){
        return $this->belongsTo('App\User');
    }

    public function racer(){
        return $this->hasMany('App\Racer');
    }
}

And this is the Event model:

public function race(){
        return $this->hasOne('App\Race');
    }

    public function racers(){
        return $this->hasMany('App\Racer');
    }

And this is what my EventsController looks like with the working alternative method and the commented not working.

public function upcoming(){
        $id = auth()->user()->id;
        $user = User::find($id);
        $pigeons = DB::table('racers')->leftJoin('pigeons', 'racers.pigeon_id', '=', 'pigeons.id')->where('pigeons.user_id', '=', $id)->get();

        //$pigeons = $user->racers;

        return view('events.upcoming')->with('pigeons', $pigeons);
    }

This is what I get with $user->racers or $user->racers()->get():

[{"id":1,"pigeon_id":14,"user_id":4,"event_id":1,"position":0,"created_at":null,"updated_at":null},{"id":2,"pigeon_id":15,"user_id":4,"event_id":1,"position":0,"created_at":null,"updated_at":null},{"id":3,"pigeon_id":16,"user_id":4,"event_id":1,"position":0,"created_at":null,"updated_at":null}]

And this is what I want to get, its not correct either since I should get id:1 but I want to pass to view these additional datas aswell like gender, color, ability (but they are in pigeons table not in racers).

[{"id":14,"pigeon_id":14,"user_id":4,"event_id":1,"position":0,"created_at":"2018-09-27 10:01:04","updated_at":"2018-09-27 10:01:04","gender":"hen","color":"blue","ability":38},{"id":15,"pigeon_id":15,"user_id":4,"event_id":1,"position":0,"created_at":"2018-09-27 10:01:04","updated_at":"2018-09-27 10:01:04","gender":"hen","color":"blue","ability":48},{"id":16,"pigeon_id":16,"user_id":4,"event_id":1,"position":0,"created_at":"2018-09-27 10:01:04","updated_at":"2018-09-27 10:01:04","gender":"cock","color":"blue","ability":11}]

Upvotes: 0

Views: 896

Answers (1)

Ojo Tokunbo
Ojo Tokunbo

Reputation: 56

To get the pigeons, what you would have to do is $pigeons = $user->racers()->get();. You can see an example of this in Laravel's official documentation https://laravel.com/docs/5.5/eloquent-relationships#introduction.

Upvotes: 1

Related Questions