我想在查询构建器中使用这样的数据库查询:
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/