From 1ee019a37fcc39a7bf0c54819c705b07035e2e19 Mon Sep 17 00:00:00 2001 From: jedi Date: Sun, 5 Jan 2020 22:26:18 +0100 Subject: [PATCH] add number of items in a container to the container endpoints --- app/Container.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/app/Container.php b/app/Container.php index 26d63e4..d00ac89 100644 --- a/app/Container.php +++ b/app/Container.php @@ -4,6 +4,7 @@ namespace App; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; +use Illuminate\Support\Facades\DB; class Container extends Model { @@ -26,4 +27,16 @@ class Container extends Model * @var array */ protected $hidden = ['created_at', 'deleted_at', 'updated_at']; + + static function all($columns=Array()){ + return Container::join('items','items.cid','=','containers.cid') + ->select('items.cid', 'name', DB::raw('count(*) as itemCount')) + ->groupBy('items.cid', 'name')->get(); + } + + static function find($id){ + return Container::join('items','items.cid','=','containers.cid') + ->select('items.cid', 'name', DB::raw('count(*) as itemCount')) + ->groupBy('items.cid', 'name')->where('items.cid', '=', $id)->first(); + } }