我有3個表posts
,tags
,和一個數據透視表post_tag
。爲什麼在Laravel中附加方法返回null?
post_tag
表:
+----+-------------+--------+
| id | post_id | tag_id |
+----+-------------+--------+
| 1 | 1 | 2 |
| 2 | 2 | 2 |
| 3 | 3 | 1 |
| 4 | 4 | 1 |
| 5 | 4 | 3 |
| 6 | 5 | 3 |
+----+-------------+--------+
我有這樣的Post
模式:
class Post extends Model {
public function tags() {
return $this->belongsToMany('Tag');
}
}
而這Tag
模式:
class Tag extends Model {
public function posts() {
return $this->belongsToMany('Post');
}
}
現在在我的控制,我想將記錄插入數據透視表post_tag
:
$post = Post::find(4);
$post_tag = $post->tags()->attach(2);
return $post_tag //it returns null
所以,我需要在數據透視表中創建記錄的細節,但$post_tag
包含null
!
如何獲取數據透視表上最後插入記錄的詳細信息(如id ,...
)?
是的你是對的。但是我認爲這很糟糕,'attach'方法不會返回任何東西! –