2014-01-05 29 views
1

MySQL表: 帖子 標籤 post_tag(與列POST_ID和TAG_ID)許多一對多(belongsToMany)Laravel關係似乎並不HHVM下工作

// Post.php model 
class Post extends Eloquent { 
    protected $table = 'posts'; 
    public $timestamps = true; 

    protected $guarded = ['id']; 

    public function tags() 
    { 
     return $this->belongsToMany('Tag'); 
    } 
} 

// Tag.php model 
class Tag extends Eloquent { 
    protected $table = 'tags'; 
    public $timestamps = false; 

    protected $guarded = ['id']; 

    public function posts() 
    { 
     return $this->belongsToMany('Post'); 
    } 
} 

// on routes.php 
Route::get('/', function() 
{ 
    $post = Post::find(44); 
    echo $post . '<br>'; 

    $tag = Tag::find(28); 
    echo $tag; 

    return $post->tags; 
}); 
擊球時

/它打印後:44,它打印標籤:28和給出

ErrorException不能使用的標量值作爲數組

訪問Post.php標記()函數上的標記屬性時。 請記住,在post_tag的表格中有一個post_id = 44和tag_id = 28的條目,但它可能爲空。 Laravel/php在嘗試訪問belongsToMany('Tag')方法時給了我這個錯誤。

我究竟做錯了什麼?

+0

我一直在進一步研究的問題納入框架的代碼發現,如果我而不是右像這樣的關係,它的工作原理: 公共功能標籤() \t { \t \t回$這個 - > belongsToMany( '標籤',NULL,NULL,NULL,「什麼功能於這裏而不是空「); \t} – mwm

+0

嘿,HHVM準備好了嗎? –

回答

0

我相信你需要使用預先加載:

$post = Post::with('tag')->find(28); 

我沒有在我面前一laravel設置進行測試。所以find可能不是可鏈接的,所以你可能需要這樣做:

$post = Post::with(array('tag' => function($query) 
{ 
    $query->where('id', '=', 44); 
}))->get(); 
+0

這是一個HHVM問題。這在Laravel中是多對多的。應該開箱即用。我忘了我使用hhvm而不是php。它現在不適用於所有的情況。 – mwm