php - Laravel: Eloquent 'more than' 和 'lesser than'

标签 php mysql laravel eloquent laravel-5.5

我想在查询构建器中使用这样的数据库查询:

SELECT * FROM posts WHERE active = 1 AND published <= '{$now}' LIMIT 5

我做了什么:

$now = new Carbon;
$feed = Post::where([
        ['active' => 1],
        ['published' => $now]
    ])
    -> take(5)
    -> get()
    -> toArray();

但它就像:

SELECT * FROM posts WHERE active = 1 AND published = '{$now}' LIMIT 5

如何制作< , <= , > , >= , <>LIKE带有 ::where 的语句方法?

最佳答案

像这样使用 ['published','>=',$now]

$now = new Carbon;
$feed = Post::where([
    ['active', '=', '1'],
    ['published','>=',$now]
])
->take(5)
->get()
->toArray();

或者使用单独的where函数

$now = new Carbon;
$feed = Post::where('active', 1)->where('published','>=', $now)
->take(5)
->get()
->toArray();

关于php - Laravel: Eloquent 'more than' 和 'lesser than',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47349695/

相关文章:

php - MySQL 最后一个条目 PHP 按日期排序

php - 无法登录 MySQL/mariaDB - [错误] 无法加载资源 : the server responded with a status of 500 (Internal Server Error) (get_data. php)

php - 在括号内使用 COALESCE 错误

Laravel Livewire,两个Livewire组件之间的通信

laravel - 在 Laravel 中使用 Sendmail 发送电子邮件的正确配置是什么?

php - 从带有复选框的表单中提取特定值以在 PHP 中选择行

PHP:清除 cron 临时文件的好脚本?

php - 网站设计中的图像

mysql - 计算重复项中的唯一值

mysql - 在MySQL中的字符串中多次连接一个变量