英文:
firstOrCreate add record only if date is different
问题
我尝试仅在created_at
列包含不同日期时添加记录,因此我不需要检查小时和分钟,只需检查年、月和日。
我所做的是:
$currentDate = Carbon::now()->format('Y-m-d');
$ret = Configuration::firstOrCreate(
[
'user_email' => $request->input('user_email'),
'created_at' => $currentDate
],
[
'price' => $request->input('price'),
'data' => $json_code,
'encoded_data' => base64_encode($json_code),
]
);
不幸的是,这段代码似乎不起作用,记录总是被添加。
我怀疑Laravel也在检查小时和分钟。
是否有一种方法可以使用firstOrCreate
仅检查Y-m-d
来添加记录?
提前感谢。
英文:
I'm trying to add a record only if created_at
column contains a different date, so I don't need to check for hour and minutes, but only for year, month and day.
What I did is:
$currentDate = Carbon::now()->format('Y-m-d');
$ret = Configuration::firstOrCreate(
[
'user_email' => $request->input('user_email'),
'created_at' => $currentDate
],
[
'price' => $request->input('price'),
'data' => $json_code,
'encoded_data' => base64_encode($json_code),
]
);
unfortunately, this code doesn't seems to work, the record is always added.
I suspect that laravel is doing the check on the hours and minutes also.
Is there a way to use firstOrCreate
to add a record checking only Y-m-d
?
Thanks in advance.
答案1
得分: 1
请尝试以下代码:
Configuration::whereDate('created_at', Carbon::now()->format('Y-m-d'))
->firstOrCreate(
[
'user_email' => $request->input('user_email'),
],
[
'price' => $request->input('price'),
'data' => $json_code,
'encoded_data' => base64_encode($json_code),
]
);
英文:
try this
Configuration::whereDate('created_at', Carbon::now()->format('Y-m-d'))
->firstOrCreate(
[
'user_email' => $request->input('user_email'),
] ,
[
'price' => $request->input('price'), 'data' => $json_code,
'encoded_data' => base64_encode($json_code)
]);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论