Search code examples
databaselaraveldatediff

How to calculate the number of days left in laravel for all rows from database


I am trying to create a laravel based project, In that I have a section to show the days remaining in... I have 3 columns in my db; they are

  • sub_start_date (which means the subscription start date)
  • sub_end_date (which means the subscription end date)
  • subscription_type (it should be a select box in that 1 month, 2 months and 3 months).

In that the sub_end_date is calculated automatically with the help of jQuery. So it will be stored in the db As 1-9-2020 (M-D-Y).

So my problem is I cannot count the days remaining. I have googled and get somewhat similar solution that I will add below but it's only for one row (or for the row that we will add at the last). And in my blade.php page it was showing the same result in a loop..

//subscription_details.blade.php
@extends('layouts.app')
@section('body')
<center>
</br>
<div class="col-lg-5">
    <h4>subscribtion details</h4></br>
<table class="table table-hover">
  <thead>
    <tr>
      <th scope="col">Name</th>
      <th scope="col">Phone Number</th>
      <th scope="col">Type Of Subscription</th>
      <th scope="col">Start Date</th>
      <th scope="col">End Date</th>
      <th scope="col">Days Remaining</th>
    </tr>
  </thead>
  <tbody>
    <tr>
    @foreach($customer as $row)
      <th scope="row">{{$row->name}}</th>
      <td>{{$row->phone}}</td>
      <td>{{$row->subscription}}Month</td>
      <td>{{$row->sub_start_date}}</td>
      <td>{{$row->sub_end_date}}</td>
      <td>{{$count}}</td> 
   </tr>
    @endforeach
</tbody>
</table> 
</div>
</center>
@endsection

My controller code is

//SubscriptionController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\customer;
use Carbon\Carbon ;

class SubscriptionController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    { 
     $now = Carbon::now();
     $customer  =customer::all();
     foreach($customer as $row)
     {
     $end_date = $row->sub_end_date;
     $cDate = Carbon::parse($end_date);
     $count = $now->diffInDays($cDate );
    }
     
return view('subscription_details',compact('customer','count'));
      }

As of now I am getting this

Subscription details

Results are like

Sub_start date = 2019-12-01
Sub_end date = 2019-12-01

Days remaining is 260 days for every column.

Please help me to find a solution


Solution

  • Defined a mutator attribute in your customer model:

    use Carbon\Carbon;
    ...
    public function getRemainingDaysAttribute()
    {
    
        if ($this->sub_end_date) {
            $remaining_days = Carbon::now()->diffInDays(Carbon::parse($this->sub_end_date));
        } else {
            $remaining_days = 0;
        }
        return $remaining_days;
    }
    

    And In your controller, just take your customers:

    public function index()
    { 
        $customer  =customer::all();
        return view('subscription_details',compact('customer'));
    }
    

    And In your view:

        @foreach($customer as $row)
          <th scope="row">{{$row->name}}</th>
          <td>{{$row->phone}}</td>
          <td>{{$row->subscription}}Month</td>
          <td>{{$row->sub_start_date}}</td>
          <td>{{$row->sub_end_date}}</td>
          <td>{{$row->remaining_days}}</td> 
       </tr>
        @endforeach