将查询翻译成Laravel雄辩的

时间:2019-02-14 07:08:40

标签: php laravel

我有一个查询:

public function upShops() {
DB:statement('update shops set position = 
(SELECT FIND_IN_SET( CONCAT(points, '_', created_at), 
(SELECT GROUP_CONCAT( CONCAT(points, '_', created_at) ORDER BY points desc, created_at desc ) FROM (select * from shops) as shop_rankings)))');
}

该查询如何转换为雄辩的Laravel?我认为有必要这样做:

Shop::update(['position' => '(SELECT FIND_IN_SET( CONCAT(points, '_', 
created_at), (SELECT GROUP_CONCAT( CONCAT(points, '_', created_at)'])-
>orderByDesc('points')->orderByDesc('created_at');

但这不起作用。更新方法出现错误。我怎样才能雄辩正确地做?

2 个答案:

答案 0 :(得分:0)

更新雄辩的方法返回一个布尔值,否则返回true,否则返回true并返回错误,因此您无法将诸如orderByDesc之类的方法链接到它。 如何更新的示例如下:

Shop::update(['position' => 'value']);

答案 1 :(得分:0)

如何处理(为了更清楚起见,我在子查询的选择中添加了表别名)?

\DB::table("shops")
    ->update([
        "position" => \DB::raw("(SELECT FIND_IN_SET( CONCAT(shops.points, '_', shops.created_at), 
    (SELECT GROUP_CONCAT( CONCAT(shop_rankings.points, '_', shop_rankings.created_at) ORDER BY points desc, created_at desc ) FROM (select * from shops) as shop_rankings)))")
]);