Laravel 可以链接多少个关系

Laravel how many relationship can be chained

我有一个由用户组成的数据库,users_child,child。

我在用户和 users_child 之间创建一对多关系,然后在 users_child 和 child 之间创建关系。现在下面的代码可以工作了:

    $test = users::find(1)->users_child
    $test1= users_child::find(1)->child

现在我想知道是否可以创建一行 link 三个 table 像这样:

    $test = users::find(1)->users_child->child

我在模型中创建了关系,但在数据库中我没有创建外键,这是个问题吗?在模型上,我为 link table.

指定字段

http://laravel.com/docs/5.1/eloquent-relationships#querying-relations

您可以像这样链接关系:

$user = Users::with("users_child.child")->where("id",1)->first();

每个点都表示存储在第一个中的关系。

用户 users_child 将被删除,用户 users_child child 将被删除。 (关系)

foreach($user->users_child as $user_child) {
    $user_child->child;
}

将为您提供所需的数据。