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();
});
如果您已经定义了关系,可以很容易地获得'订单'的所有'收据'。在这里查看文档https://laravel.com/docs/5.4/eloquent-relationships –
你的查询似乎是正确的可能你有'receipt_id'所有'订单'与'状态1' 如果我错了发布一些你的示例数据生病让你查询 –
@SagarGautam我只想查询“订单”表。也许这很难想象,所以我编辑了我的原始帖子,显示了我的刀片的样子。 –