【问题标题】:Laravel: Eloquent 'more than' and 'lesser than'Laravel:雄辩的“大于”和“小于”
【发布时间】:2017-11-17 11:32:50
【问题描述】:

我想要在查询生成器中这样的数据库查询:

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语句?

【问题讨论】:

标签: php mysql laravel eloquent laravel-5.5


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    口才

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

    【讨论】:

      【解决方案3】:

      使用这个

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

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

      【讨论】:

        猜你喜欢
        • 2017-10-31
        • 2018-01-10
        • 2020-01-21
        • 1970-01-01
        • 2020-02-11
        • 2015-12-23
        • 2015-03-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多