Reputation: 720
So I have table named "orders" that has receipt_id to link it with the table named "receipts"
I want to group the orders that has the same receipt_id, so in my controller, I used this code;
$orders = DB::table('orders')
->where('status', 0)
->groupBy('receipt_id')
->get();
return $orders;
But unfortunately, it only returns the first one. I have a feeling that I shouldn't be using groupBy.
This is what it currently returns;
{
id: 1,
item: "12",
qty: 123,
price: "1823.00",
subtotal: "123.00",
receipt_id: 123,
status: 0,
created_at: null,
updated_at: null
}
EDIT:
I only want to query the receipts table, here's what my blade looks like right now and commented what I want to accomplish.
@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: Database structure for receipts and orders.
receipts;
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();
});
orders;
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();
});
Upvotes: 0
Views: 2138
Reputation: 720
Somehow I managed to solve my problem and I'll share my codes of course. :)
controller;
$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>
Upvotes: 1