add number of items in a container to the container endpoints

This commit is contained in:
j3d1 2020-01-05 22:26:18 +01:00
parent 47dcc79752
commit 1ee019a37f

View file

@ -4,6 +4,7 @@ namespace App;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;
class Container extends Model class Container extends Model
{ {
@ -26,4 +27,16 @@ class Container extends Model
* @var array * @var array
*/ */
protected $hidden = ['created_at', 'deleted_at', 'updated_at']; 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();
}
} }