Php 拉威尔多对多不工作
我有一个基本的多对多关系:Php 拉威尔多对多不工作,php,mysql,laravel,Php,Mysql,Laravel,我有一个基本的多对多关系: Posts Tags 我有一个数据透视表,名为post\u tag 我正在尝试返回视图文件中的所有给定POST标记,如下所示: @foreach($posts as $post) {{ dd($post->tags) }} @endforeach 我得到以下错误: SQLSTATE[42S22]:未找到列:1054未知列“tags.post_id” 在“where子句”(SQL:select*fromtagswheretagspost\u id
Posts
Tags
我有一个数据透视表,名为post\u tag
我正在尝试返回视图文件中的所有给定POST标记,如下所示:
@foreach($posts as $post)
{{ dd($post->tags) }}
@endforeach
我得到以下错误:
SQLSTATE[42S22]:未找到列:1054未知列“tags.post_id”
在“where子句”(SQL:select*fromtags
wheretags
post\u id
=
1和标记
post\u id
不为空(视图:
C:\wamp\www\laravel\resources\views\posts\index.blade.php)
以下是我的模型:
class Post extends Model
{
....
public function tags() {
return $this->hasMany(\App\Models\Tag::class);
}
}
class Tag extends Model
{
....
public function posts() {
return $this->belongsToMany(\App\Models\Post::class);
}
}
你知道这是怎么回事吗?pivot表中有数据,但关系似乎工作不正常。两个关系都应该使用belongToMany
class Post extends Model
{
....
public function tags() {
return $this->belongsToMany(\App\Models\Tag::class);
}
}
两种关系都应该使用belongtomany
class Post extends Model
{
....
public function tags() {
return $this->belongsToMany(\App\Models\Tag::class);
}
}
您应该在Post模型中使用
belongTomany
,请检查
您应该在Post模型中使用
belongTomany
,请检查
枢轴选项卡和外键的名称必须在模型中定义 类后扩展模型 { } 类标记扩展了模型 {
}必须在模型中定义枢轴选项卡和外键的名称 类后扩展模型 { } 类标记扩展了模型 {
}你就是那个人!我认为这是有道理的,因为一篇帖子可以有很多标签,而标签属于很多帖子。当它允许我的时候,我会接受的。谢谢你就是那个人!我认为这是有道理的,因为一篇帖子可以有很多标签,而标签属于很多帖子。当它允许我的时候,我会接受的。谢谢
public function tags() {
return $this->belongsToMany(\App\Models\Tag::class,'post_tag','tag_id',post_id);
}
public function Posts() {
return $this->belongsToMany(\App\Models\Tag::class,'post_tag','post_id',tag_id);
}