简体   繁体   English

通过hasManyThrough Laravel Eloquent获取多个表

[英]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. 我试图从登录的用户那里获取所有赛车手,并且它正常工作,但是如果我输入$pigeons = $user->racer我只会得到赛车手表。 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. 我已经使查询生成器可以保留加入表的功能,但是我不确定为什么不能使用Laravel内部方法来建立这种关系。

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. 这就是我的EventsController的样子,它具有替代方法,注释不起作用。

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(): 这是我用$ user-> racers或$ 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}] [{ “ID”:1, “pigeon_id”:14, “USER_ID”:4 “event_id的”:1, “位置”:0, “created_at”:空 “的updated_at”:空},{ “ID”: 2, “pigeon_id”:15, “USER_ID”:4 “event_id的”:1, “位置”:0, “created_at”:空 “的updated_at”:空},{ “ID”:3 “pigeon_id”: 16, “USER_ID”:4 “event_id的”:1, “位置”:0, “created_at”:空 “的updated_at”:空}]

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:1,但我也希望通过查看这些附加数据,例如性别,肤色,能力(但它们在赛鸽表中而不是在赛车手中)。

[{"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}] [{“ 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“,”性别“:”母鸡“,”颜色“:”蓝色“,”能力“:38},{” id“:15,” pigeon_id“: 15,“ user_id”:4,“ event_id”:1,“位置”:0,“ created_at”:“ 2018-09-27 10:01:04”,“ updated_at”:“ 2018-09-27 10:01 :04" , “性别”: “母鸡”, “色彩”: “蓝”, “能力”: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”, “颜色”: “蓝”, “能力”:11}]

To get the pigeons, what you would have to do is $pigeons = $user->racers()->get(); 要获得鸽子,您需要做的是$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 . 您可以在Laravel的官方文档https://laravel.com/docs/5.5/eloquent-relationships#introduction中看到一个示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM