2017-07-26 206 views
0

所以我有一個名爲「訂單」已receipt_id將它與命名的錶鏈接「收據Laravel。查詢生成器GROUPBY

我想組具有相同receipt_id,所以在我的控制器訂單表,我用這個代碼;

 $orders = DB::table('orders') 
     ->where('status', 0) 
     ->groupBy('receipt_id') 
     ->get(); 

    return $orders; 

但不幸的是,它只返回第一個。我有一種感覺,我不應該使用groupBy。

這是它目前返回的;

{ 
id: 1, 
item: "12", 
qty: 123, 
price: "1823.00", 
subtotal: "123.00", 
receipt_id: 123, 
status: 0, 
created_at: null, 
updated_at: null 
} 

編輯:

我只想查詢收據表,這裏就是我的刀的樣子,現在和評論我要完成的任務。

@forelse($orders as $order) 
     <div class="col-md-4"> 
      <div class="panel panel-default"> 
       <div class="panel-heading text-center"> 
        {{$order->receipt_id}} 
       </div> 

       <div class="panel-body text-center"> 
        <table class="table table-bordered table-responsive"> 
         <thead> 
          <th>Order</th> 
          <th>Quantity</th> 
         </thead> 

         <tbody> 

          //all orders that has the same receipt_id will be showed here. 
          <tr> 
           <td>{{$order->item}}</td> 
           <td>{{$order->qty}}</td> 
          </tr> 
         </tbody> 
        </table> 
       </div> 

       <div class="panel-footer clearfix"> 
        <button type="submit" class="btn btn-success pull-right" style="margin-right: 10px;"> 
         <i class="fa fa-check-circle" aria-hidden="true"></i> Served 
        </button> 
       </div> 
      </div> 
     </div> 
     @empty 

     @endforelse 

編輯:收據和訂單的數據庫結構。

收據;

Schema::create('receipts', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->decimal('total'); 
      $table->decimal('vatable'); 
      $table->decimal('vat'); 
      $table->decimal('vat_exempt'); 
      $table->decimal('vat_zero'); 
      $table->decimal('amount_due'); 
      $table->decimal('cash'); 
      $table->decimal('change_due'); 
      $table->integer('receipt_id'); 
      $table->timestamps(); 
     }); 

訂單;

Schema::create('orders', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->string('item'); 
      $table->integer('qty'); 
      $table->decimal('price'); 
      $table->decimal('subtotal'); 
      $table->integer('receipt_id'); 
      $table->boolean('status'); 
      $table->timestamps(); 
     }); 
+0

如果您已經定義了關係,可以很容易地獲得'訂單'的所有'收據'。在這裏查看文檔https://laravel.com/docs/5.4/eloquent-relationships –

+0

你的查詢似乎是正確的可能你有'receipt_id'所有'訂單'與'狀態1' 如果我錯了發佈一些你的示例數據生病讓你查詢 –

+0

@SagarGautam我只想查詢「訂單」表。也許這很難想象,所以我編輯了我的原始帖子,顯示了我的刀片的樣子。 –

回答

0

不知何故,我設法解決了我的問題,我會分享我的代碼當然。 :)

控制器;

$receipt_ids = DB::table('orders') 
      ->where('status', 0) 
      ->orderBy('receipt_id', 'asc') 
      ->groupBy('receipt_id') 
      ->get(); 

     $orders = DB::table('orders') 
      ->where('status', 0) 
      ->orderBy('receipt_id', 'asc') 
      ->get(); 

     return view('kitchens.index', compact('receipt_ids','orders')); 

blade;

@extends('layouts.app') 

<div class="panel panel-default"> 
    <div class="panel-body" style="overflow: scroll; height:85vh;"> 
     <div class="row"> 
      @forelse($receipt_ids as $receipt_id) 
       <div class="col-md-4"> 
        <div class="panel panel-default"> 
         <div class="panel-heading text-center"> 
          {{$receipt_id->receipt_id}} 
         </div> 

         <div class="panel-body"> 
          <table class="table table-bordered table-responsive"> 
           <thead> 
            <th>Name</th> 
            <th>Quantity</th> 
           </thead> 

           <tbody> 
           @foreach($orders as $order) 
            @if($receipt_id->receipt_id == $order->receipt_id) 
             <tr> 
              <td>{{$order->item}}</td> 
              <td>{{$order->qty}}</td> 
             </tr> 
            @endif 
           @endforeach 
           </tbody> 
          </table> 
         </div> 

         <div class="panel-footer clearfix"> 
          <button type="submit" class="btn btn-success pull-right" style="margin-right: 10px;"> 
           <i class="fa fa-check-circle" aria-hidden="true"></i> Served 
          </button> 
         </div> 
        </div> 
       </div> 
      @empty 

      @endforelse 
     </div> 
    </div> 
</div>