我在商店模型中有关系评论,并且在收藏商店中附加了计数评论:
商店型号:
class Shop extends Model
{
...
public static function boot() {
parent::boot();
static::saved(function() {
static::updateRanks();
});
}
protected $withCount = ['reviews']
public function reviews() {
return $this->hasMany(Review::class);
}
}
我尝试在查询中添加reviews_count
:
public static function updateRanks()
{
DB::statement("update shops set position = (SELECT FIND_IN_SET( CONCAT(points, '_', reviews_count, '_', created_at), (SELECT GROUP_CONCAT( CONCAT(points, '_', reviews_count, '_', created_at) ORDER BY points desc, reviews_count desc, created_at desc ) FROM (select * from shops) as shop_rankings)))");
}
但是我得到了错误:
SQLSTATE[42000]: Syntax error or access violation: 1583 Incorrect parameters in the call to native function
'concat...
我该如何解决?我需要INNER JOIN或子查询来查询店铺的数量吗?保存商店时,我会按订购点,reviews_count和created_at来更新排名。