如何使用laravel 5.2从MYSQL数据库表中获取时间总和

时间:2017-04-10 07:40:02

标签: date laravel-5

我有一个名为total_work_hours的列,它是登录时间和注销时间/日之间的差异。我需要计算一周或我们从界面发送的日期之间的总工作时间。我怎样才能让它发挥作用。

这是我的代码:

控制器:

public function search(request $request)
{
    $search= DB::table('attendance')->orderBy('Id', 'desc')->where('user',$request->id)->whereBetween('Date', [$request->Fdate, $request->Tdate])->get();

    return Response::json(array(
        'results' => $search,
        'pdays' => sizeof($search),
    ));
}
public function getAttencance(){
    $data=DB::table('users')->where('Type',"E")->get();
    return view('admin.attendanceSummary')->with('data',$data);
}

现在我只发送包含行数的完整结果。与此同时,我需要发送总工时。我的其他细节如下。

路线:

Route::post('search',['as'=>'search','uses'=>'Admin\AdminController@search']);
Route::get('viewAttendance',['as' => 'viewAttendance','uses' =>'Admin\AdminController@getAttencance']);

前端:

    @extends('layouts.mainPage')

@section('content')
  <Style>
     .help-inline {
         color: red;
         font-weight: 700;
     }
  </Style>


  <script type="text/javascript">
    $(document).ready(function () {
        $( "#Fdate" ).datepicker({ dateFormat: "yy-mm-dd" });
        $( "#Tdate" ).datepicker({ dateFormat: "yy-mm-dd" });
        $('#b-summary').DataTable();

        $("#menu-toggle").click(function(e) {
          e.preventDefault();
          $("#wrapper").toggleClass("toggled");
        });
    });
    function search() {
      var Fdate=$('#Fdate').val();
      var Tdate=$('#Tdate').val();
      if(Fdate =='' || Tdate == '')
      {
        if(Fdate == '' && Tdate != '')
        {
             $('#spanFro').removeClass('hidden');
             $('#spanTo').addClass('hidden');
         }else if(Tdate == '' && Fdate != '')
         {
             $('#spanTo').removeClass('hidden');
             $('#spanFro').addClass('hidden');
         }
         else {
             $('#spanFro').removeClass('hidden');
             $('#spanTo').removeClass('hidden');
         }

      }
      else {
       $('#spanFro').addClass('hidden');
       $('#spanTo').addClass('hidden');
       var id = $("#sel1").val();

       var postdata = {};
       postdata['id'] = id;
       postdata['Fdate'] = $('#Fdate').val();
       postdata['Tdate'] = $('#Tdate').val();
       postdata["_token"] = $('input[name=_token]').val();

       $.ajax({
           url: 'search',
           type: "post",
           data: postdata,
           success: function (data) {
               //success
               $('#pDay').val(data.pdays);
               var t = $('#b-summary').DataTable();
               var total_work_hrs=null;

               /*for (var i = 0; i < data.results.length; i++) {
                   total_work_hrs =[data.results[i].WorkTime];
                 }*/
               t.clear().draw();
               for (var i = 0; i < data.results.length; i++) {
                   var row =
                           [
                               dateFormat(data.results[i].Date, "yyyy-mm-dd"),
                               dateFormat(data.results[i].InTime, "h:MM"),
                               dateFormat(data.results[i].OutTime, "h:MM"),
                               data.results[i].InIP,
                               data.results[i].OutIP,
                               data.results[i].TotalBreaks,
                               data.results[i].WorkTime
                           ];
                   var r = t.row.add(row).draw(false);
                   if (r) {
                       $(r.node()).data("row", data.results[i]);
                   }
               }

           },
          error: function (data) {
               //failur logic
          }
        })
      }
    }
  </script>

    <div id="page-content-wrapper">
      <div class="container-fluid">
        <div class="row" style="margin-top: 70px;">
          <div class="col-lg-12">
            <div class="cover-head">
              <h1>Employees Details</h1>
            </div>
            <div class="panel panel-default">
              <div class="panel-heading">Filter Attendance</div>
              <div class="panel-body">
                <input type="hidden" id="_token" name="_token" value="{{ csrf_token() }}">
                <div class="form-group col-sm-4">
                  <label for="sel1">Emplayee list:</label>
                  <select class="form-control " id="sel1">
                      @if(sizeof($data)!=0)
                          @foreach($data as $row)
                          <option value="{{$row->id}}">{{$row->FirstName}}{{$row->LastName}}</option>
                          @endforeach
                      @endif
                  </select>
                </div>
                <div class="form-group  col-sm-3">
                  <label for="Fdate">From Date:</label>
                  <input type="text" class="form-control" required placeholder="From date" id="Fdate">
                  <span class="help-inline hidden" id="spanFro">Please Enter From Date</span>
                </div>
                <div class="form-group  col-sm-3">
                  <label for="Tdate">To Date:</label>
                  <input type="text" class="form-control" required id="Tdate" placeholder="To date">
                  <span class="help-inline hidden" id="spanTo">Please Enter To date</span>
                </div>
                <button class="btn btn-success" id="search" onclick="search();" style="margin-top: 24px;">Search</button>
              </div>
            </div>

            <div class="panel panel-default">
              <div class="col-lg-4">Days Present:
                <input class="form-control col-lg-4" type="text" name="presentDays" readonly placeholder="Present days" id="pDay">
              </div>
              <div class="table-responsive panel-body">
                <table id='b-summary' class="table table-bordered">
                    <thead>
                      <tr>
                        <th>Login Date</br> [ Y-M-D ]</th>
                        <th>Login Time</br> [ Hrs:Min ]</th>
                        <th>Logout Time</br> [ Hrs:Min ]</th>
                        <th>Logged In From</th>
                        <th>Logged Out From</th>
                        <th>Total Breaks</th>
                        <th>Total Work Time</th>
                      </tr>
                    </thead>
                    <tbody>

                    </tbody>
                  </table>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

@endsection

0 个答案:

没有答案