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();
});
リレーションシップを定義していれば、「注文」のすべての領収書を取得するのは簡単です。ここのドキュメントを参照してくださいhttps://laravel.com/docs/5.4/eloquent-relationships –
あなたのクエリが正しいと思われる 'status_1'を持つ' orders'のすべてに対して 'receipt_id'を同じにしてもいいかもしれません サンプルデータがあなたにクエリをさせます –
@SagarGautam私は "orders"テーブルを照会したいだけです。たぶんこれは想像がつきにくいので、私のオリジナルの投稿を編集して、私のブレードの外観を見せてくれました。 –