这是我的原始查询 .
DB :: raw(“SELECT * FROM tble WHERE status = 1 and now()BETWEEN start_time和end_time ORDER BY id DESC LIMIT 1”)
如何将其转换为Laravel Eloquent?
DB::table('tble')::where('status',1) ->where('start_time', '<=', Carbon::now()) ->where('end_time', '>=', Carbon::now()) ->orderBy('id') ->first();
您可以使用简单的日期处理包Carbon来实现此目的 .
试试这个
DB::table('tble')::where('status',1) ->whereBetween(Carbon::now(), ['start_time', 'end_time']) ->orderBy('id') ->first();
为表创建模型 app/Table.php
app/Table.php
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Table extends Model { protected $table = 'tble'; }
逻辑
$now = \Carbon\Carbon::now(); $result = \App\Table::where('status', 1) ->where('start_time', '<=', $now) ->where('end_time', '>=', $now) ->orderBy('id') ->first();
3 回答
您可以使用简单的日期处理包Carbon来实现此目的 .
试试这个
为表创建模型
app/Table.php
逻辑