2017-04-10 128 views
0

從MYSQL數據庫表中獲得時間總和我有一個名爲total_work_hours的列,它是登錄時間和登出時間/日期之間的差異。我需要計算一個星期的總工作時間或從接口發送的日期之間的總工作時間。我怎樣才能使它工作。如何使用laravel 5.2

這裏是我的代碼:

控制器:

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\[email protected]']); 
Route::get('viewAttendance',['as' => 'viewAttendance','uses' =>'Admin\[email protected]']); 

前端:

@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

對錶格佈局的描述對於回答這個問題非常有用。 –

+0

表:出勤 色譜柱: ID INT(11)AI PK, 用戶INT(11), 日期日期, 銀泰日期時間, OutTime日期時間, InIP VARCHAR(15), OutIP VARCHAR(15), TotalBreaks int(34), WorkTime time, BreakFlag int(11), 備註varchar(60) –

回答

0

我改了一下我的控制器。我仍然無法獲得輸出。

public function search(request $request) 
{ 
    $worktime= DB::table('attendance')->where('user',$request->id) 
      ->whereBetween('Date', [$request->Fdate, $request->Tdate]) 
      ->selectRaw("time(sum(WorkTime)) as wrktime") 
      ->get(); 

    $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), 
     'twt' => $worktime->wrktime, 
    )); 
} 

發送空值。這有什麼不對?