2017-07-26 12 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 
} 

EDIT:

私だけ領収書テーブルを照会するには、ここに私のブレードは、今のように見えるものだと私は達成したいものをコメントしています。

@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 

EDIT:領収書とオーダーのデータベース構造。

領収書;

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

あなたのクエリが正しいと思われる 'status_1'を持つ' orders'のすべてに対して 'receipt_id'を同じにしてもいいかもしれません サンプルデータがあなたにクエリをさせます –

+0

@SagarGautam私は "orders"テーブルを照会したいだけです。たぶんこれは想像がつきにくいので、私のオリジナルの投稿を編集して、私のブレードの外観を見せてくれました。 –

答えて

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

ブレード;

@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> 
関連する問題