c3lf-system-3/app/Container.php

42 lines
1.1 KiB
PHP

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;
class Container extends Model
{
use SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'cid', 'name'
];
protected $primaryKey = 'cid';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['created_at', 'deleted_at', 'updated_at'];
static function all($columns=Array()){
return Container::leftJoin('items','items.cid','=','containers.cid')
->select('containers.cid', 'name', DB::raw('count(items.iid) as itemCount'))
->groupBy('containers.cid', 'name')->get();
}
static function find($id){
return Container::leftJoin('items','items.cid','=','containers.cid')
->select('containers.cid', 'name', DB::raw('count(items.iid) as itemCount'))
->groupBy('containers.cid', 'name')->where('items.cid', '=', $id)->first();
}
}