2015-03-25 52 views
0

我創建了一個數據透視表post_tag,並使用同步方法將數據插入數據。在laravel 4.2中提取數據透視表數據

現在我想要提取後的各自的標籤視圖顯示像下面

{{ $post->tags }} 

我怎樣才能做到這一點?提前致謝。

+0

現在,你得到的對象。對 ? – 2015-03-25 10:34:35

回答

1

你必須將你的對象轉換爲數組。在laravel中,你可以像這樣使用。

{{ $post->tags->toArray() }} 

有關documentation的更多詳情。

編輯

在發表你的模型。

public function tags() 
{ 
    return $this->hasMany('Tag'); // One to Many 

    return $this->belongsToMany('Tag'); // Many to many 
} 

你可以這樣得到。

$tags = Post::find($postId)->tags; 

或者

$post = Post::find($postId); 
$tags = $post->tags; 

希望它會爲你工作。

+0

恐怕你誤解我的問題,因爲我的錯誤措辭/引用。 它實際上並不關心我得到的對象或數組。我只想從樞軸訪問標記顯示在我的視圖。我應該怎麼做? – 2015-03-25 10:43:16

+0

我已經更新了我的答案。 – 2015-03-25 10:47:28