发现评论有多个标签。在多对多多态 laravel 关系中
find reviews has more than one tag. in many to many polymorphic laravel relation
你好我正在尝试使用多对多多态但不知何故它不起作用
我无法在多对多多态关系中获得相关评论
我想通过标签获得评论,由客户选择
Table结构:
review
> id - integer
> name - string
tags
> id - integer
> name - string
taggables
> tag_id - integer
> taggable_id - integer
> taggable_type - string
型号:
class Tag extends Eloquent
{
public function reviews()
{
return $this->morphedByMany(Review::class, 'taggable');
}
}
class Review extends Eloquent
{
public function tags()
{
return $this->morphToMany(Tag::class, 'taggable');
}
}
the request from customer [tag_id_1,tag_id_2,tag_id_3,tag_id_4]
请求如 [1, 2, 3, 4, 5]
标签键数组
如果与此标签相关的评论找到并获得评论,我会尝试类似的方法
return 相关评论的代码:
return Review::join('taggables', 'taggables.taggable_id', '=', 'reviews.id')
->where('taggables.taggable_type', '=', Review::class)
->whereIn('taggables.tag_id', [1, 2, 3, 4, 5])
->groupBy('reviews.id')
->orderBy('name', 'asc')
->get();
或:
Review::WhereHas('tags', function ($query) {
$query->whereIn('tags_id', [1, 2, 3, 4, 5]);
})->get();
我需要的结果:
唯一应该有这些标签的评论
review:{
name: "Review",
tags :[1, 2, 3, 4, 5]
}
你的 query.correct 形式有错别字 taggables.tagable_id
应该是 taggables.taggable_id
。我不知道这是否是问题所在,但建议您编写如下代码。
在审查模型中定义这样的关系:
public function tags()
{
return $this->morphToMany(Tag::class, 'taggable','taggables','taggable_id','tag_id');
}
并且在 Tag 模型中定义如下关系:
public function reviews()
{
return $this->morphedByMany(Review::class, 'taggable','taggables','tag_id','taggable_id');
}
当您想要 return 评论特定标签时,请这样做:
$tag=Tag::find(1);
$tagReviews=$tag->reviews;
如果您想要包含所有标签的评论,这是一种方法:
$tagIds = [1,2,3,4,5];
$reviews = Review::query();
foreach($tagIds as $id){
$reviews->whereHas('tags', function($query) use ($id) {
return $query->where('id', $id);
});
}
$reviewsWithAllThoseIds = $reviews->get();
//if you don't want to use foreach.
collect($tagIds)->each(function($id) use ($reviews){
$reviews->whereHas('tags', function($q) use ($id) {
return $q->where('id', $id);
});
});
这应该会为您提供包含数组中 id 的所有标签的评论。
你好我正在尝试使用多对多多态但不知何故它不起作用 我无法在多对多多态关系中获得相关评论 我想通过标签获得评论,由客户选择
Table结构:
review
> id - integer
> name - string
tags
> id - integer
> name - string
taggables
> tag_id - integer
> taggable_id - integer
> taggable_type - string
型号:
class Tag extends Eloquent
{
public function reviews()
{
return $this->morphedByMany(Review::class, 'taggable');
}
}
class Review extends Eloquent
{
public function tags()
{
return $this->morphToMany(Tag::class, 'taggable');
}
}
the request from customer [tag_id_1,tag_id_2,tag_id_3,tag_id_4]
请求如 [1, 2, 3, 4, 5] 标签键数组
如果与此标签相关的评论找到并获得评论,我会尝试类似的方法
return 相关评论的代码:
return Review::join('taggables', 'taggables.taggable_id', '=', 'reviews.id')
->where('taggables.taggable_type', '=', Review::class)
->whereIn('taggables.tag_id', [1, 2, 3, 4, 5])
->groupBy('reviews.id')
->orderBy('name', 'asc')
->get();
或:
Review::WhereHas('tags', function ($query) {
$query->whereIn('tags_id', [1, 2, 3, 4, 5]);
})->get();
我需要的结果: 唯一应该有这些标签的评论
review:{
name: "Review",
tags :[1, 2, 3, 4, 5]
}
你的 query.correct 形式有错别字 taggables.tagable_id
应该是 taggables.taggable_id
。我不知道这是否是问题所在,但建议您编写如下代码。
在审查模型中定义这样的关系:
public function tags()
{
return $this->morphToMany(Tag::class, 'taggable','taggables','taggable_id','tag_id');
}
并且在 Tag 模型中定义如下关系:
public function reviews()
{
return $this->morphedByMany(Review::class, 'taggable','taggables','tag_id','taggable_id');
}
当您想要 return 评论特定标签时,请这样做:
$tag=Tag::find(1);
$tagReviews=$tag->reviews;
如果您想要包含所有标签的评论,这是一种方法:
$tagIds = [1,2,3,4,5];
$reviews = Review::query();
foreach($tagIds as $id){
$reviews->whereHas('tags', function($query) use ($id) {
return $query->where('id', $id);
});
}
$reviewsWithAllThoseIds = $reviews->get();
//if you don't want to use foreach.
collect($tagIds)->each(function($id) use ($reviews){
$reviews->whereHas('tags', function($q) use ($id) {
return $q->where('id', $id);
});
});
这应该会为您提供包含数组中 id 的所有标签的评论。