douwen9343 2018-09-27 12:30
浏览 136

通过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}]

  • 写回答

1条回答 默认 最新

  • dpb35161 2018-09-27 12:51
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?