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

Laravel: Eloquent 'more than' and 'lesser than'

我想要在查询构建器中进行这样的数据库查询:

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

如何使用 ::where 制作 <<=>>=<>LIKE 语句方法?

这样使用['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();

Eloquent

  $feed = Post::where('active','=',1)->where('published','<=',$now)->get();

使用这个

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

请看说明书:https://laravel.com/docs/master/queries#raw-expressions