在laravel 5.3上使用SqliteDriver
表cpjobs
...
$table->timestamp('created_at')->nullable(false); // saved data unixtime
$table->timestamp('delayed_at')->nullable(true); // saved data unixtime
...
| id | delayed_at | start_at |
|-----| ---------- | ----------- |
| 2 | null | 1477912725 |
| 3 | 1478007928 | null |
| 4 | null | null |
如果在修补程序上运行
Cpjob::where('delayed_at', '>', '1478007902')->first();
=> App\Cpjob {#694
id: "11",
type: "backup",
created_at: "1477911786",
delayed_at: "1478007928",
}
但如果为delayed_at>运行复合体now和start_at null,返回空结果,必须返回id为4的行
Cpjob::where([['delayed_at', '<', '1477912725'],['start_at', null]])->first();
=> null
如果生成SQL ...
>>> Cpjob::where([['delayed_at', '<', '1478007734'],['start_at', null]])->toSql();
=> "select * from "cpjobs" where ("delayed_at" < ? and "start_at" is null)"
我已尝试过几种方式来发送时间,unix时间戳,碳,但所有,产生相同的&lt;吗
答案 0 :(得分:1)
您可以尝试使用whereNull()
:
Cpjob::where(fuction($q) {
$q->where('delayed_at', '<', '1477912725')
->orWhereNull('delayed_at');
})
->whereNull('start_at')
->first();
答案 1 :(得分:0)
好的,谢谢你的提示。
创建我的本地范围
应用程序/ Cpjob.php
public function scopeFirstJob($query,$unixTime)
{
return $query->whereNull('start_at')->where(function ($q) use ($unixTime) {
$q->where('delayed_at', '<', $unixTime)->orWhereNull('delayed_at');
})->first();
}
致电我的命令
$job = Cpjob::firstJob($unixtime)