Skip to content

Instantly share code, notes, and snippets.

@daemswibowo
Last active October 1, 2019 04:19
Show Gist options
  • Save daemswibowo/18993b9345bda04a0999cfa17e5ef770 to your computer and use it in GitHub Desktop.
Save daemswibowo/18993b9345bda04a0999cfa17e5ef770 to your computer and use it in GitHub Desktop.
Group Count Invoices
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Invoice extends Model
{
public function bulan ()
{
return $this->hasMany('App\Invoice', 'bill_year','bill_year');
}
public function scopeWithBulan ($q, $status = 0)
{
return $q->whereStatus($status)->with(['bulan' => function ($q) use ($status) {
$q->select('bill_month','bill_year', \DB::raw('count(id) as jumlah'))->groupBy('bill_year','bill_month')->whereStatus($status);
}]);
}
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Invoice;
class InvoiceController extends Controller
{
public function index ()
{
$invoices_true = Invoice::select('bill_year', \DB::raw('count(id) as jumlah'))->groupBy('bill_year')->withBulan(1)->get();
$invoices_false = Invoice::select('bill_year', \DB::raw('count(id) as jumlah'))->groupBy('bill_year')->withBulan(0)->get();
$items = ["TRUE" => $invoices_true, "FALSE" => $invoices_false];
return $items;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment