I have 3 tables posts
,tags
, and a pivot table post_tag
.
post_tag
table is :
+----+-------------+--------+
| id | post_id | tag_id |
+----+-------------+--------+
| 1 | 1 | 2 |
| 2 | 2 | 2 |
| 3 | 3 | 1 |
| 4 | 4 | 1 |
| 5 | 4 | 3 |
| 6 | 5 | 3 |
+----+-------------+--------+
I have this in Post
model :
class Post extends Model {
public function tags() {
return $this->belongsToMany('Tag');
}
}
And this in Tag
model :
class Tag extends Model {
public function posts() {
return $this->belongsToMany('Post');
}
}
Now in my Controller I'm trying to insert a record to the pivot table post_tag
:
$post = Post::find(4);
$post_tag = $post->tags()->attach(2);
return $post_tag //it returns null
So, I need to get details of created record in pivot table, but $post_tag
contains null
!
How can I get the details(like id ,...
) of last inserted record on the pivot table?