0

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.

wartortle
  • 3
  • 2
  • 6
  • 1
    I think you are looking for this? https://stackoverflow.com/questions/39508963/calculate-difference-between-two-dates-using-carbon-and-blade – VMeijer Jun 01 '21 at 15:00
  • Does this answer your question? [Calculate difference between two dates using Carbon and Blade](https://stackoverflow.com/questions/39508963/calculate-difference-between-two-dates-using-carbon-and-blade) – N69S Jun 01 '21 at 15:03
  • should I add this under the store function in controller and call the $total_days in my view? sorry if I look like a dummy cuz i'm very new to this T_T Application::create([ $user_id= auth()->user()->id; $leave_id= $request->leave_id; $dateFrom = $request->date_from; $dateTo = $request->date_to $date1 = new DateTime($dateFrom); $date2 = new DateTime($dateTo); $interval = $date1->diff($date2); $total_days = $interval->format('%a'); ]); return redirect()->route('admin.leaverequest.index') ->with('success', 'Leave Application Submitted'); – wartortle Jun 01 '21 at 15:29

2 Answers2

0

Since you are new to laravel .Here is the answer.I assume date format of date_from and date_to is month/date/year

 $dateFrom=\Carbon\Carbon::createFromFormat('m/d/Y',$request->date_from);
  $dateTo=\Carbon\Carbon::createFromFormat('m/d/Y',$request->date_to);

    if($dateTo>$dateFrom){
        dd($dateFrom->diffInDays($dateTo));
    }

The above code will return number of days between two dates

Updated

public function store(Request $request)
   {
          $dateFrom=\Carbon\Carbon::createFromFormat('m/d/Y',$request->date_from);
  $dateTo=\Carbon\Carbon::createFromFormat('m/d/Y',$request->date_to);

  
        Application::create([
            'user_id'=>auth()->user()->id,
            'leave_id'=>$request->leave_id,
            'date_from'=>$request->date_from,
            'date_to'=>$request->date_to,
            'days'=> $dateFrom->diffInDays($dateTo),
        ]);
       return redirect()->route('admin.leaverequest.index')
            ->with('success', 'Leave Application Submitted');

    }
John Lobo
  • 14,355
  • 2
  • 10
  • 20
  • i have done this but the no. of days doesn't stored in database – wartortle Jun 01 '21 at 16:41
  • 'days'=> $request->days, to 'days'=>$dateFrom->diffInDays($dateTo), – John Lobo Jun 01 '21 at 16:47
  • the solution at the above should be put before Application::create or inside it? it did show anything :((( – wartortle Jun 01 '21 at 17:28
  • @wartortle.upted my answr – John Lobo Jun 02 '21 at 02:51
  • this workssss!!!!!!! thank you so much :) Feeling happy although i have long way to go and explore until i finish this project. – wartortle Jun 02 '21 at 05:55
  • @wartortle.your welcome.no wrries.if you google you will get lots of resources for laravel.sometimes it might not be exactly same what we wanted.so better you debug line by line when you get issues.so you can learn more deeper into it.at the beginning i also in same path.no worries. If struck in between stack overflow help you to solve your issues – John Lobo Jun 02 '21 at 10:38
0
 $date1 = new DateTime('2021-02-01');
 $date2 = new DateTime('2021-06-01');
 $interval = $date1->diff($date2);
 $interval->format('%a')+1;

Use the above function. It will give you the days between two dates.