私は3つのテーブルposts
,tags
、ピボットテーブルpost_tag
を持っています。Laravelでattachメソッドが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
モデルでこれを持っている:Tag
モデルで
class Post extends Model {
public function tags() {
return $this->belongsToMany('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'メソッドは何も返しません! –