Laravel Carbon 使用 sameDay() 查询日期
Laravel Carbon Querying date with sameDay()
我正在尝试在 Laravel (5.8) 中建立一个查询方法来检索当前日期的条目(想想来自 Facebook 的 "Discover this day",有点相同)。
我有以下代码:
$todayMementos = Memento::where(['user_id' => auth()->user()->id])
->whereDate('date', Carbon::isSameDay())
->get();
我尝试用这个函数比较日期;我正在为它使用 Carbon "isSameDay()",不确定它是否是正确选择的函数,但我想要的结果是这样的:
如果在这个 DD-MM-no-matter-the-year,用户添加了一些条目,给我。
建议?提前致谢!
您可以使用碳日期格式来获取 yyyy-mm-dd 格式的当前日期。 sql.
中的数据库使用此格式
$todayMementos = Memento::where(['user_id' => auth()->user()->id])
->whereDay('date',\Carbon\Carbon::now()->day)
->whereMonth('date',\Carbon\Carbon::now()->month)
->get();
检索当前日期和月份
$day = Carbon::now()->day;
$month = Carbon::now()->month;
您可以使用 Eloquent 过滤器来检索同一天和同一月。
$todayMementos = Memento::where(['user_id' => auth()->user()->id])
->whereDay('date', '=' $day)
->whereMonth('date', '=', $month)
->get();
我正在尝试在 Laravel (5.8) 中建立一个查询方法来检索当前日期的条目(想想来自 Facebook 的 "Discover this day",有点相同)。
我有以下代码:
$todayMementos = Memento::where(['user_id' => auth()->user()->id])
->whereDate('date', Carbon::isSameDay())
->get();
我尝试用这个函数比较日期;我正在为它使用 Carbon "isSameDay()",不确定它是否是正确选择的函数,但我想要的结果是这样的:
如果在这个 DD-MM-no-matter-the-year,用户添加了一些条目,给我。
建议?提前致谢!
您可以使用碳日期格式来获取 yyyy-mm-dd 格式的当前日期。 sql.
中的数据库使用此格式$todayMementos = Memento::where(['user_id' => auth()->user()->id])
->whereDay('date',\Carbon\Carbon::now()->day)
->whereMonth('date',\Carbon\Carbon::now()->month)
->get();
检索当前日期和月份
$day = Carbon::now()->day;
$month = Carbon::now()->month;
您可以使用 Eloquent 过滤器来检索同一天和同一月。
$todayMementos = Memento::where(['user_id' => auth()->user()->id])
->whereDay('date', '=' $day)
->whereMonth('date', '=', $month)
->get();