在 Laravel 框架中的 where 函数中使用来自 Carbon 包

我想查看我 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')不正确,什么是正确的代码?


慕雪6442864
浏览 217回答 1
1回答

繁华开满天机

该parse函数需要一个字符串 date 将其解析为 Carbon 对象,而不是列名。所以你应该把你的支票改成这样:$orders = Order::where('created_at' ,'>=', now()->subMinutes(30))->get();我now()在 Laravel 中使用了辅助函数,它等于Carbon::now()
打开App,查看更多内容
随时随地看视频慕课网APP