Laravel:Eloquent'超过'和'小于'

时间:2017-11-17 11:32:50

标签: php mysql laravel eloquent laravel-5.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::whereUPDATE your_table SET displayorder = displayorder + X * C; 语句?

3 个答案:

答案 0 :(得分:3)

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

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

或使用单独的函数

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

答案 1 :(得分:1)

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

答案 2 :(得分:1)

使用此

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

请参阅手册:https://laravel.com/docs/master/queries#raw-expressions