php - Laravel. Query Builder groupBy -
so have table named "orders" has receipt_id link table named "receipts"
i want group orders has same receipt_id, in controller, used code;
$orders = db::table('orders') ->where('status', 0) ->groupby('receipt_id') ->get(); return $orders;
but unfortunately, returns first one. have feeling shouldn't using groupby.
this 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 want query receipts table, here's blade looks right , commented want accomplish.
@forelse($orders $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 has same receipt_id 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 receipts , 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(); });
somehow managed solve problem , i'll share 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 $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 $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>
Comments
Post a Comment