当我尝试通过 post 访问类别时类别关系出错
Category relationship is giving error when I try to acces category through post
当我尝试访问文章 vie 类别时它正常工作这里是 category.php
模型
public function articles(){
return $this->belongsToMany('App\Article');
}
但是当我尝试访问类别名称视图文章时它无法正常工作,我犯了一个错误并试图修复它,但到目前为止还没有成功。这是文章模型
public function category(){
return $this->hasOne('App\category');
}
并且有一个 table 用于将这两者相互关联,它在我获取给定标签的所有文章的页面中称为 article_category
,我想做类似 $article->category->name
对我来说有些事情听起来很不对劲,但我无法弄清楚。
我收到的错误是
Column not found: 1054 Unknown column 'category.article_id' in 'where clause' (SQL: select * from category
where category
.article_id
= 1 and category
.article_id
is not null limit 1) (View: C:\wamp64\www\loremipsum\bluhbluhbluh\articleByTag.blade.php)
顺便说一句,我在创建文章时为 article_category
保存关系的方式是
$article->category()->sync($request->category, false);
您建立的关系有误。去掉中间的table,在文章table.
中添加category_id
关系设置不正确
Category
模型应该是这样的:
class Category extends Model
{
protected $table='category';
//give me all articles associated with the given category
public function articles()
{
return $this->hasMany('App\Article');
}
}
和Article
模型
class Article extends Model
{
protected $table='article';
//get the category associated with the given article
public function category()
{
return $this->belongsTo('App\Category');
}
}
用于将文章附加到类别:
$article->category()->associate($category)->save();
您说过那篇文章可能只有一个类别。在这种情况下,删除 pivot table 并将 category_id
添加到 articles
table.
然后在Article
模型中定义这个关系:
public function category()
{
return $this->belongsTo(Category::class);
}
并且在 Category
模型中:
public function articles()
{
return $this->hasMany(Article::class);
}
完成后,您将能够通过以下方式访问文章的类别:
$article->category->name
当我尝试访问文章 vie 类别时它正常工作这里是 category.php
模型
public function articles(){
return $this->belongsToMany('App\Article');
}
但是当我尝试访问类别名称视图文章时它无法正常工作,我犯了一个错误并试图修复它,但到目前为止还没有成功。这是文章模型
public function category(){
return $this->hasOne('App\category');
}
并且有一个 table 用于将这两者相互关联,它在我获取给定标签的所有文章的页面中称为 article_category
,我想做类似 $article->category->name
对我来说有些事情听起来很不对劲,但我无法弄清楚。
我收到的错误是
Column not found: 1054 Unknown column 'category.article_id' in 'where clause' (SQL: select * from
category
wherecategory
.article_id
= 1 andcategory
.article_id
is not null limit 1) (View: C:\wamp64\www\loremipsum\bluhbluhbluh\articleByTag.blade.php)
顺便说一句,我在创建文章时为 article_category
保存关系的方式是
$article->category()->sync($request->category, false);
您建立的关系有误。去掉中间的table,在文章table.
中添加category_id
关系设置不正确
Category
模型应该是这样的:
class Category extends Model
{
protected $table='category';
//give me all articles associated with the given category
public function articles()
{
return $this->hasMany('App\Article');
}
}
和Article
模型
class Article extends Model
{
protected $table='article';
//get the category associated with the given article
public function category()
{
return $this->belongsTo('App\Category');
}
}
用于将文章附加到类别:
$article->category()->associate($category)->save();
您说过那篇文章可能只有一个类别。在这种情况下,删除 pivot table 并将 category_id
添加到 articles
table.
然后在Article
模型中定义这个关系:
public function category()
{
return $this->belongsTo(Category::class);
}
并且在 Category
模型中:
public function articles()
{
return $this->hasMany(Article::class);
}
完成后,您将能够通过以下方式访问文章的类别:
$article->category->name