S. M. Shahinul Islam
S. M. Shahinul Islam

Reputation: 2800

Fetch pivot table data in laravel 4.2

I have created a pivot table post_tag and inserting data into it using sync method.

Now I want to fetch respective tags of a post to show in the view like following

{{ $post->tags }}

How can I do this? Thanks in advance.

Upvotes: 0

Views: 53

Answers (1)

Arkar Aung
Arkar Aung

Reputation: 3584

You have to convert your object to array. In laravel, you can use like this.

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

For more detail in documentation.

Edit

In your Post model.

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

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

You can get like this.

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

Or

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

Hope it will be work for you.

Upvotes: 1

Related Questions