Laravel 拉维关系:hasManyThrough、belongsTo、belongstomy

Laravel 拉维关系:hasManyThrough、belongsTo、belongstomy,laravel,laravel-5,Laravel,Laravel 5,嘿,我这里有一个拉威尔项目,你们能帮我解决这个关于关系的问题吗 我有以下数据库结构: users id name email password event id description city_id block_range id event_id user_block_ranges user_id block_range_id 解释 用户:普通用户身份验证表。(与用户块范围有归属关系) 事件:存储事

嘿,我这里有一个拉威尔项目,你们能帮我解决这个关于关系的问题吗

我有以下数据库结构:

users
    id
    name
    email
    password

event
    id
    description
    city_id

block_range
    id
    event_id

user_block_ranges
    user_id
    block_range_id
解释

用户
:普通用户身份验证表。(与
用户块范围有
归属关系

事件
:存储事件信息。(与
块范围
有许多关系)

block\u range
:保存事件时间块。(与
事件
以下关系)

真正的问题是:如何获取用户的所有事件?通过
user\u block\u range
然后
block\u range
关系?可能使用
hasManyThrough


提前谢谢。

我相信你们的模型是这样的:

用户模型

class User extends Model
{
    public function blockRanges()
    {
        return $this->belongsToMany('App\BlockRange', 'user_block_ranges', 'user_id', 'block_range_id');
    }
}
class BlockRange extends Model
{
    public function event()
    {
        return $this->belongsTo('App\Event');
    }
}
块范围模型

class User extends Model
{
    public function blockRanges()
    {
        return $this->belongsToMany('App\BlockRange', 'user_block_ranges', 'user_id', 'block_range_id');
    }
}
class BlockRange extends Model
{
    public function event()
    {
        return $this->belongsTo('App\Event');
    }
}
要获取用户的所有事件,可以执行以下操作:

$user = App\User::find(1);

$events = array();

foreach ($user->blockRanges as $block_range) {
    $events = $block_range->event;
}

你们和这个问题中描述的一样,只是不同,你们将有4条链elements@Aleksandrs即使有很多桌子,我也能喜欢这个答案?这对我很有用,非常感谢。我真的很感激你的回答,但是,没有办法用Laravel ORM做到这一点(不幸的是,我还没有找到一种简单的方法,只能通过拉威尔关系来做到这一点:(