我想制作一个日期选择器(日历)来根据时间戳(Laravel 6)过滤我的数据库记录



如何为我的报告数据添加日期选择器 (Laravel6( 我仍然不知道从哪里开始。

这是我的报告控制器(索引(:

public function index()
{
$reports=Report::orderBy('id', 'DESC')->paginate(10);
return view('reports.index',['reports' => $reports]);
}

这是我在 report.index html 上的索引

<div class="table-responsive">
<table class="table align-items-center table-flush">
<thead class="thead-light">
<tr>
<th scope="col">Name</th>
<th scope="col">Student number</th>
<th scope="col">Transaction</th>
<th scope="col">Remarks</th>
<th scope="col">Queue number</th>
<th scope="col">Created at</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
@foreach ($reports as $report)
<tr>
<td>{{ $report->name}}</td>
<td>{{ $report->snumber}}</td>
<td>{{ $report->transaction}}</td>
<td>{{ $report->remarks}}</td>
<td>{{ $report->letter}}-{{ $report->number}}</td>
<td>{{ $report->created_at }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<div class="card-footer py-4">
<nav class="d-flex justify-content-end" aria-label="...">
{{ $reports->links() }}
</nav>
</div>

我的数据库表如下所示:

Schema::create('reports', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('snumber');
$table->string('name');
$table->string('email');
$table->string('mobile');
$table->string('department');
$table->string('letter');
$table->integer('number');
$table->string('transaction');
$table->string('remarks')->nullable();
$table->timestamps();
$table->softDeletes();
});

请帮助我。谢谢!

要首先过滤数据,您必须将datepicker放在包含数据的表之外form标记中。

如果要使用 ajax 调用,请传递包含过滤日期的表单值,并且在同一index方法中,您可以检查此调用是否为 ajax 调用。

如果 ajax 调用,则用过滤后的数据覆盖变量。

像这样的东西,

$reports=Report::orderBy('id', 'DESC')->paginate(10);
if($request->ajax()){
$reports=Report::orderBy('id', 'DESC')->whereDate('created_at', today())->paginate(10);
}
return view('reports.index',['reports' => $reports]);

如果你不想使用 Ajax 调用并想要加载页面,那么你可以传递数据并检查它是否存在,像这样,

$reports=Report::orderBy('id', 'DESC')
->when(! empty(request('filterdate')), function($q) {
return $q->whereDate('created_at', today());
})
->paginate(10);
return view('reports.index',['reports' => $reports]);

它只会在filterdate不为空时过滤日期。

希望这对:)有所帮助

最新更新