【发布时间】:2017-06-21 20:13:42
【问题描述】:
我有以下表格:
帖子{id, title, description}
标签{id, name, description}
post_tags {post_id, tag_id}
在 Laravel 中,我设置了如下关系。我不确定如何查询我的 post_tags 数据透视表。我收到以下错误:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'dev_match.post_tag' doesn't exist (SQL: select `tags`.*, `post_tag`.`post_id` as `pivot_post_id`, `post_tag`.`tag_id` as `pivot_tag_id` from `tags` inner join `post_tag` on `tags`.`id` = `post_tag`.`tag_id` where `post_tag`.`post_id` = 1)
家庭控制器:
public function getindex(){
$post = Post::all();
return view('welcome', compact('post'));
}
主页视图:
@foreach($post as $posts)
<tbody>
<tr>
<td> <a href="">{{$posts->tags->name}}</a> </td>
<td> </td>
<td> asked </td>
</tr>
</tbody>
@endforeach
标签模型:
public function post()
{
return $this->belongsToMany('App\Post');
}
帖子模型:
public function tag()
{
return $this->belongsToMany('App\Tag');
}
【问题讨论】:
标签: php laravel many-to-many relationship