Laravel - 非法偏移类型HasMany()

时间:2014-01-06 03:29:18

标签: php laravel eloquent

我的代码:

$user = User::where('uid', $user_id)->with('groups.sub_groups')->firstOrFail();
return $user;

在我的用户模型上:

return $this->hasMany('group');

在我的群组模型上:

return $this->hasMany('sub_group');

现在堆栈跟踪给出了错误:

Illegal offset type
Illuminate/Database/Eloquent/Relations/HasOneOrMany.php

    $foreign = $this->getPlainForeignKey();

    // First we will create a dictionary of models keyed by the foreign key of the
    // relationship as this will allow us to quickly access all of the related
    // models without having to do nested looping which will be quite slow.
    foreach ($results as $result)
    {
        $dictionary[$result->{$foreign}][] = $result;
    }

更新

删除.sub_group不会返回错误。

// works
$user = User::where('uid', $user_id)->with('groups')->firstOrFail();

1 个答案:

答案 0 :(得分:0)

你确定这种关系吗?

必须是hasMany且belongsTo是您的群组模型