如何计算数据库中所有原始数据在laravel中剩余的天数

问题描述 投票:0回答:1

亲爱的,

我正在尝试创建一个基于laravel的项目,因为我有一个部分来显示剩余的日子...我的数据库中有3个问题,它们是

  • sub_start_date(这表示订阅的开始日期)
  • sub_end_date(表示订阅结束日期)
  • subscription_type(应为1个月,2个月内的选择框和相应的3个月)。

因为sub_end_date是在jQuery的帮助下自动计算的。因此它将存储在数据库中1-9-2020 (M-D-Y)。所以我的问题是我无法计算剩余的天数。.我用谷歌搜索并得到了一些类似的解决方案,我将在下面添加,但仅适用于一行(或最后添加的行),并在我的blade.php中页面,它在循环中显示相同的结果。]

    //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

我的控制器代码是

//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'));
      }

截至目前,我正在得到这个

订阅详细信息

结果就像提交开始日期= 2019-12-01提交结束日期= 2019-12-01 剩余天数为每个大棚260天请帮助我找到解决方法

database laravel datediff
1个回答
0
投票

在客户模型中定义了mutator属性:

© www.soinside.com 2019 - 2024. All rights reserved.