php - 我正在使用 laravel,我想计算控制器中存储函数中两个日期之间的天数并自动显示在索引视图中
问题描述
这是我的请假应用控制器,请假的所有功能都在这里。我想计算用户输入的日期天数,将天数存储在数据库中并将其显示在视图刀片中。
class LeaveApplicationController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$leaverequest = Application::with('users', 'leaves')
->where('user_id', auth()->user()->id)
->get();
return view('admin.leaverequest.index', compact('leaverequest'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$user = User::pluck('name', 'id');
$leave = Leave::pluck('leave_type', 'id');
return view('admin.leaverequest.create', compact('user', 'leave'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//dd($request);
Application::create([
'user_id'=>auth()->user()->id,
'leave_id'=>$request->leave_id,
'date_from'=>$request->date_from,
'date_to'=>$request->date_to,
'days'=> $request->days,
]);
return redirect()->route('admin.leaverequest.index')
->with('success', 'Leave Application Submitted');
}
这是请假请求 index.blade.php 的视图。这里可以显示其他列中的所有数据,除了列天数。我不知道怎么称呼它。
<table class="table table-bordered">
<tr>
<th>No</th>
<th>Name</th>
<th>Leave Type</th>
<th>Date From</th>
<th>Date To</th>
<th>Days Taken</th>
<th>Status Application</th>
</tr>
@foreach ($leaverequest as $t)
<tr>
<td>{{ $t->id }}</td>
<td>{{ $t->users->name }}</td>
<td>{{ $t->leaves->leave_type }}</td>
<td>{{ $t->date_from }}</td>
<td>{{ $t->date_to }}</td>
<td>{{ $t->total_days}}</td>
<td>
@if($t->status==0)
<span class="badge badge-pill badge-warning">Pending</span>
@elseif($t->status==1)
<span class="badge badge-pill badge-success">Approved</span>
@else
<span class="badge badge-pill badge-danger">Rejected</span>
@endif
</td>
</tr>
@endforeach
</table>
这是请假请求 create.blade.php 的视图。在这里,我希望自动计算并显示天数,但在这里我必须输入天数。
<form action="{{ route('admin.leaverequest.store') }}" method="POST">
@csrf
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<select class="form-control" name="leave_id">
<option value="">-- Choose Leave Types --</option>
@foreach ($leave as $id => $type)
<option
value="{{$id}}" {{ (isset($application['leave_id']) && $application['leave_id'] == $id) ? ' selected' : '' }}>{{$type}}</option>
</option>
@endforeach
</select>
</div>
</div>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<strong>Date From:</strong>
<input type="date" name="date_from" class="form-control" placeholder="Start Date">
</div>
</div>
</div>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<strong>Date To:</strong>
<input type="date" name="date_to" class="form-control" placeholder="End Date">
</div>
</div>
</div>
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<div class="form-group">
<strong>Days Taken</strong>
<input type="text" name="days" class="form-control" id="TotalDays" placeholder="Number of leave days">
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
<a class="btn btn-primary" href="{{ route('admin.leaverequest.index') }}"> Back</a>
</div>
</div>
</form>
@endsection
这是我第一次使用 laravel 框架,尽管我看到了一些其他的答案和指导,但我完全无法帮助自己解决这个问题。请帮帮我,谢谢。
解决方案
$date1 = new DateTime('2021-02-01');
$date2 = new DateTime('2021-06-01');
$interval = $date1->diff($date2);
$interval->format('%a')+1;
使用上述功能。它会给你两个日期之间的天数。
推荐阅读
- elasticsearch - 删除嵌套字段 Elasticsearch 映射的管道
- json - 为什么我的 json 文件只转换为空白 CSV?
- ssl - 带有证书的 C# HTTPS Post
- microservices - NestJS 网关无法接收来自微服务的消息?
- python - 来自networkx的MultiDiGraph边缘使用connectionStyle绘制
- vb.net - 从 VB6 ActiveX 调用 VB.NET COM 类库不起作用
- postgresql - docker entrypoint.sh 脚本中的 psql 登录问题
- amazon-web-services - 使用 Amazon EventBridge 在组织内发布和接收跨账户事件
- hibernate - JPA 正确定位和打包元模型
- jquery - node.js + mysql + ejs 数据插入后无需重新加载页面即可更新屏幕信息