在 Laravel 框架中的 where 函数中使用 from Carbon 包
Using from Carbon package in where function in Laravel framwork
我想查看我的订单是在 30 分钟前提交的。
我正在使用 Carbon 包来检查这项工作。
所以我使用了这段代码,但是 Laravel 出现了这个错误:
DateTime::__construct(): Failed to parse time string (created_at) at position 0 (c): The timezone could not be found in the database
$now = carbon::now();
$orders= Order::where(Carbon::parse('created_at')->addMinutes(30) ,'>=' , h)->get();
foreach($orders as $order){
if (is_null($orders)){
return 'false';
}else{
return 'true';
}
}
事实上我不确定我的代码,但是如果 where(Carbon::parse('created_at')
不正确,那么正确的代码是什么?
parse
函数需要一个字符串日期来将其解析为 Carbon 对象,而不是列名。
因此您应该将支票更改为:
$orders = Order::where('created_at' ,'>=', now()->subMinutes(30))->get();
我在 Laravel 中使用 now()
辅助函数,它等于 Carbon::now()
我想查看我的订单是在 30 分钟前提交的。
我正在使用 Carbon 包来检查这项工作。
所以我使用了这段代码,但是 Laravel 出现了这个错误:
DateTime::__construct(): Failed to parse time string (created_at) at position 0 (c): The timezone could not be found in the database
$now = carbon::now();
$orders= Order::where(Carbon::parse('created_at')->addMinutes(30) ,'>=' , h)->get();
foreach($orders as $order){
if (is_null($orders)){
return 'false';
}else{
return 'true';
}
}
事实上我不确定我的代码,但是如果 where(Carbon::parse('created_at')
不正确,那么正确的代码是什么?
parse
函数需要一个字符串日期来将其解析为 Carbon 对象,而不是列名。
因此您应该将支票更改为:
$orders = Order::where('created_at' ,'>=', now()->subMinutes(30))->get();
我在 Laravel 中使用 now()
辅助函数,它等于 Carbon::now()