2014-01-06 42 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是您的組模型