Laravel - 如何从数据库中获取最后 5 个日期
Laravel - How to get last 5 dates from database
我有一个模型 ImportantDate
架构如下
Schema::create('important_dates', function (Blueprint $table) {
$table->increments('id');
$table->date('date');
$table->text('description');
$table->timestamps();
$table->softDeletes();
});
我想得到:
1) 当前日期(今天)的最后五个日期和
2) 从当前日期(今天)起接下来的五天
示例数据:
id Date description
1 2017-12-20 some description
2 2017-12-25 some description
3 2018-01-02 some description
4 2018-01-28 some description
5 2018-02-02 some description
6 2018-02-12 some description
7 2018-02-18 some description
8 2018-02-28 some description
9 2018-03-02 some description
10 2018-03-12 some description
在我的 ImportantDatesController.php
中,我尝试了一个显示当前日期最近日期的查询,但我需要从今天开始的最后 5 个日期和接下来的 5 个日期。
public function index()
{
$upcoming_dates = ImportantDate::orderByRaw(' abs( timestampdiff(second, current_timestamp, created_at)) asc ')
->limit(5)
->get();
$recent_dates = ImportantDate::orderByRaw(' abs( timestampdiff(second, current_timestamp, created_at)) dsc ')
->limit(5)
->get();
return view('pages.dashboard', compact('upcoming_dates','recent_dates'));
}
期待急需的帮助。
谢谢。
您可以使用 where()
子句来 compare
您的日期是 upcoming
日期或 previous
日期。然后你可以使用 orderBy()
根据你的日期获取最新的 5。
$upcoming_dates = ImportantDate::where('date','>', date('Y-m-d'))->orderBy('date')
->limit(5)
->get();
$recent_dates = ImportantDate::where('date','<', date('Y-m-d'))->orderBy('date', 'DESC')
->limit(5)
->get();
DATE('Y-m-d')
将给出给定格式的当前日期。如果您需要获取当前日期,可以使用 add >=
或 <=
$upcoming_dates = ImportantDate::where('date' , '>', DATE('Y-m-d'))
->orderBy('id', asc)
->limit(5)
->get();
$recent_dates = ImportantDate::where('date', '<', DATE('Y-m-d'))
->orderBy('id', desc)
->limit(5)
->get();
SELECT *
FROM table
WHERE id IN (SELECT id FROM table WHERE datetime = (SELECT MAX(datetime) FROM table))
ORDER BY id DESC
LIMIT 1
我有一个模型 ImportantDate
架构如下
Schema::create('important_dates', function (Blueprint $table) {
$table->increments('id');
$table->date('date');
$table->text('description');
$table->timestamps();
$table->softDeletes();
});
我想得到:
1) 当前日期(今天)的最后五个日期和
2) 从当前日期(今天)起接下来的五天
示例数据:
id Date description
1 2017-12-20 some description
2 2017-12-25 some description
3 2018-01-02 some description
4 2018-01-28 some description
5 2018-02-02 some description
6 2018-02-12 some description
7 2018-02-18 some description
8 2018-02-28 some description
9 2018-03-02 some description
10 2018-03-12 some description
在我的 ImportantDatesController.php
中,我尝试了一个显示当前日期最近日期的查询,但我需要从今天开始的最后 5 个日期和接下来的 5 个日期。
public function index()
{
$upcoming_dates = ImportantDate::orderByRaw(' abs( timestampdiff(second, current_timestamp, created_at)) asc ')
->limit(5)
->get();
$recent_dates = ImportantDate::orderByRaw(' abs( timestampdiff(second, current_timestamp, created_at)) dsc ')
->limit(5)
->get();
return view('pages.dashboard', compact('upcoming_dates','recent_dates'));
}
期待急需的帮助。
谢谢。
您可以使用 where()
子句来 compare
您的日期是 upcoming
日期或 previous
日期。然后你可以使用 orderBy()
根据你的日期获取最新的 5。
$upcoming_dates = ImportantDate::where('date','>', date('Y-m-d'))->orderBy('date')
->limit(5)
->get();
$recent_dates = ImportantDate::where('date','<', date('Y-m-d'))->orderBy('date', 'DESC')
->limit(5)
->get();
DATE('Y-m-d')
将给出给定格式的当前日期。如果您需要获取当前日期,可以使用 add >=
或 <=
$upcoming_dates = ImportantDate::where('date' , '>', DATE('Y-m-d'))
->orderBy('id', asc)
->limit(5)
->get();
$recent_dates = ImportantDate::where('date', '<', DATE('Y-m-d'))
->orderBy('id', desc)
->limit(5)
->get();
SELECT *
FROM table
WHERE id IN (SELECT id FROM table WHERE datetime = (SELECT MAX(datetime) FROM table))
ORDER BY id DESC
LIMIT 1