2014-01-06 31 views
0

我的代码:Laravel - 非法偏移类型的hasMany()

$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(); 

回答

0

你确定这种关系吗?

必须hasMany和belongsTo是您的组模型