This is my Leave Application Controller where all the function of leave is here. I want to calculate the number of days of date that user input that store the days in database and show it in view blade.
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');
}
This is view for leave request index.blade.php. Here all data in other column can be shown except for column days taken. I don't know how to call it.
<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>
This is view for leave request create.blade.php. Here i want the number of days is automatically calculated and shown but here i have to input the number of days.
<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
This is my first time using laravel framework and I am totally couldn't help myself to solve this although I have see some other answers and guidance. Please help me, thank you.