Laravel原始查询问题

时间:2017-06-29 12:07:07

标签: php laravel eloquent

我的2个查询有问题:

查询#1:

<!DOCTYPE html>
<html>
<head>
<title>Bug?</title>
</head>
<body>

<input type="text" />
<br /><br />
<button>Click me to focus on the input</button>
<br /><br />
<input type="text" />

<script type="text/javascript">
    document.getElementsByTagName('input')[0].addEventListener('blur', function(e){
        console.log('blur', e);
    });

    document.getElementsByTagName('button')[0].addEventListener('mousedown', function(e){
        console.log('mousedown', e);

        document.getElementsByTagName('input')[0].focus();
    });
</script>
</body>
</html>

是否有可能通过雄辩来写这个?

第二个:

$rows = DB::table(self::TABLE_NAME)
        ->selectRaw('subforum_id, count(1) as count')
        ->groupBy('subforum_id')
        ->get();

我需要在列posts_count中添加+1 - 如何执行此操作?

有人可以解释一下RAW的用途吗?

2 个答案:

答案 0 :(得分:1)

您可以编写RAW查询,如:

DB::table(DB::raw("Update topic set posts_count = posts_count + 1"))

RAW查询几乎在每个框架中都可用,它用于进行复杂查询,其中框架方法不足以进行查询。

答案 1 :(得分:0)

你可以简单地使用这个

凭借雄辩: Topic::find($topicId)->increment('posts_count');

或者使用查询构建器:

DB::table('topics')->where(['id'=>$topicId])->increment('posts_count');

根据laravel查询构建器文档https://laravel.com/docs/5.4/queries#increment-and-decrement