Created
September 15, 2016 10:39
-
-
Save tolawho/df3e9f28852ccc55e0a019c2caa32011 to your computer and use it in GitHub Desktop.
Model user, PSR2 coding standard.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Auth\UserTrait; | |
use Illuminate\Auth\UserInterface; | |
use Illuminate\Auth\Reminders\RemindableTrait; | |
use Illuminate\Auth\Reminders\RemindableInterface; | |
use Zizaco\Entrust\HasRole; | |
class User extends Eloquent implements UserInterface, RemindableInterface | |
{ | |
use UserTrait, RemindableTrait, HasRole; | |
/** | |
* The database table used by the model. | |
* | |
* @var string | |
*/ | |
protected $table = 'users'; | |
protected $primaryKey = 'id'; | |
public static $rules = array( | |
'name' => 'required|min:5|max:20', | |
'display' => 'max:20', | |
'email' => 'required|email|unique:users', | |
'username' => 'required|alpha_num|between:5,20', | |
'password' => 'required|alpha_num|between:6,12', | |
'become' => 'required|in:0,1,2', | |
'accept' => 'required|accepted', | |
'title' => 'required|between:20,100', | |
'gender' => 'required|in:0,1', | |
'id_card' => 'required|numeric', | |
'birthday' => 'required|date_format:d/m/Y', | |
'phone' => 'required', | |
'address' => 'required', | |
'city' => 'required|exists:cities,id', | |
'state' => 'required|exists:states,id', | |
'country' => 'required|exists:countries,id', | |
'hour_rate' => 'required|integer|min:50000', | |
'latlng' => 'required|not_in:[lat:lng]', | |
'degree' => 'required|exists:experiences,id', | |
'school' => 'required|exists:schools,id', | |
'subject' => 'required', | |
'skill' => 'required', | |
'about' => 'required|min:200', | |
); | |
/** | |
* The attributes excluded from the model's JSON form. | |
* | |
* @var array | |
*/ | |
protected $hidden = array('password', 'remember_token'); | |
public function jobs() | |
{ | |
return $this->hasMany('Job'); | |
} | |
public function tags() | |
{ | |
return $this->morphToMany('Tag', 'taggable'); | |
} | |
public function times() | |
{ | |
return $this->morphToMany('Time', 'timeable'); | |
} | |
public function photos() | |
{ | |
return $this->morphMany('Photo', 'imageable'); | |
} | |
public function subjects() | |
{ | |
return $this->morphToMany('Subject', 'subjectable'); | |
} | |
/** | |
* Get display name | |
* | |
* @author tolawho | |
* @param string | |
* @return string | |
*/ | |
public function getDisplayNameAttribute($v) | |
{ | |
return $v?$v:$this->name; | |
} | |
/** | |
* Get avatar url | |
* | |
* @author tolawho | |
* @param string $v | |
* @return string | |
*/ | |
public function getAvatarAttribute($v) | |
{ | |
$avatar = $v?$v:$this->name; | |
return '/img/avatar/'.$avatar; | |
} | |
/** | |
* Create short desc about | |
* | |
* @author tolawho | |
* @param string $v | |
* @return string | |
*/ | |
public function getShortAttribute($v) | |
{ | |
return Str::words($this->about, 60); | |
} | |
public function setShortAttribute($v) | |
{ | |
} | |
/** | |
* Format price | |
* | |
* @author tolawho | |
* @param string $v | |
* @return string | |
*/ | |
public function getRateAttribute($v) | |
{ | |
return number_format(ceil($this->hour_rate / 1000) * 1000, 0, '.', '.'); | |
} | |
/** | |
* Count tutor by city id(s) | |
* | |
* @param int|array $ids | |
* @param int $total | |
* @return \Illuminate\Support\Collection | |
*/ | |
public static function countByCityId($ids, &$total = 0) | |
{ | |
if (!$ids) { | |
return []; | |
} | |
$ids = is_array($ids) ? $ids : array($ids); | |
$qqq = self::selectRaw('teach_in, count(teach_in) as total') | |
->whereIn('teach_in', $ids)->tutor()->active() | |
->ready()->orderBy('id')->groupBy('teach_in') | |
->get()->keyBy('teach_in')->toArray(); | |
foreach ($ids as $id) { | |
if (!isset($qqq[$id])) { | |
$qqq[$id] = [ | |
'total' => 0 | |
]; | |
} | |
$total += $qqq[$id]['total']; | |
$qqq[$id]['total'] = number_format($qqq[$id]['total'], 0, ',', ','); | |
} | |
$total = number_format($total, 0, ',', ','); | |
return $qqq; | |
} | |
/** | |
* Get tutor by subject id | |
* | |
* @author tolawho | |
* @param int $city_id | |
* @return mixed | |
*/ | |
public static function countUserOfSubjectByCityId($city_id) | |
{ | |
$qqq = self::selectRaw('subject_id, count(subject_id) as total') | |
->join('subjectables', 'subjectables.subjectable_id', '=', 'users.id') | |
->where('subjectables.subjectable_type', '=', 'User'); | |
if ($city_id) { | |
$qqq->whereIn('users.teach_in', [$city_id]); | |
} | |
return $qqq->tutor()->active()->groupBy('subject_id')->lists('total', 'subject_id'); | |
} | |
/** | |
* Count user of root subject | |
* | |
* @author tolawho | |
* @param int|array $ids id of root and child | |
* @param int $city_id | |
* @return int | |
*/ | |
public static function countUserOfRootByCityId($ids, $city_id) | |
{ | |
if (!$ids) { | |
return 0; | |
} | |
$ids = is_array($ids) ? $ids : array($ids); | |
$qqq = self::selectRaw('count(DISTINCT users.id) as total') | |
->join('subjectables as sbj', 'sbj.subjectable_id', '=', 'users.id') | |
->where('sbj.subjectable_type', '=', 'User') | |
->whereIn('sbj.subject_id', $ids); | |
if ($city_id) { | |
$qqq->whereIn('users.teach_in', [$city_id]); | |
} | |
return $qqq->tutor()->active()->ready()->pluck('total'); | |
} | |
/** | |
* Get list user by subject id | |
* | |
* @author tolawho | |
* @param int $sbj_id | |
* @return array | |
*/ | |
public static function getUserIdBySubjectId($sbj_id) | |
{ | |
return self::selectRaw('users.id') | |
->join('subjectables', 'subjectables.subjectable_id', '=', 'users.id') | |
->where('subjectables.subjectable_type', '=', 'User') | |
->whereIn('subjectables.subject_id', $sbj_id) | |
->tutor()->active()->lists('users.id'); | |
} | |
/** | |
* Get list user by city_id | |
* | |
* @author tolawho | |
* @param int $city_id | |
* @return array | |
*/ | |
public static function getByCityId($city_id, &$user_ids = []) | |
{ | |
$qqq = self::select(['id']); | |
if ($city_id) : | |
$city_id = is_array($city_id) ? $city_id : array($city_id); | |
$qqq->whereIn('teach_in', $city_id); | |
endif; | |
$users = $qqq->tutor()->active()->get()->keyBy('id')->toArray(); | |
$user_ids= array_keys($users); | |
return $users; | |
} | |
/** | |
* Get total tutor | |
* | |
* @author tolawho | |
* @return int | |
*/ | |
public static function getTotal() | |
{ | |
return self::selectRaw('count(id) as total')->tutor()->pluck('total'); | |
} | |
public static function getLastest() | |
{ | |
return self::select(['id','name','username','display_name','slug']) | |
->ready()->tutor()->active()->orderBy('id', 'desc')->take(6)->get(); | |
} | |
/** | |
* Get top(6) tutor | |
* | |
* @author tolawho | |
* @return array | |
*/ | |
public static function getTop() | |
{ | |
$qqq = self::select([ | |
'users.id','users.name','users.slug', | |
'users.display_name','users.username' | |
]) | |
->join('job_applies', 'job_applies.user_id', '=', 'users.id') | |
->whereIn('users.status', ['1']) | |
->where('users.profile_updated', '1') | |
->whereIn('users.become', ['1']) | |
->where('job_applies.status', '=', '3') | |
->orderBy('job_applies.created_at', 'desc') | |
->take(6)->get(); | |
$total = $qqq->count(); | |
if ($total >= 6) { | |
return $qqq; | |
} else { | |
$topcnf = Config::get('base.top'); | |
return self::select([ | |
'users.id','users.name','users.slug', | |
'users.display_name','users.username' | |
]) | |
->whereIn('users.status', ['1']) | |
->where('users.profile_updated', '1') | |
->whereIn('users.become', ['1']) | |
->take(6)->get(); | |
} | |
} | |
public function scopeLike($query, $field, $value) | |
{ | |
return $query->where($field, 'LIKE', "%$value%"); | |
} | |
public function scopeSearch($query, $value) | |
{ | |
return !$value ? $query : | |
$query->where(function ($q) use ($value) { | |
$q->orWhere('title', 'LIKE', "%$value%"); | |
$q->orWhere('display_name', 'LIKE', "%$value%"); | |
$q->orWhere('about', 'LIKE', "%$value%"); | |
}); | |
} | |
public function scopeCustomer($q) | |
{ | |
return $q->whereIn('become', ['0']); | |
} | |
public function scopeTutor($q) | |
{ | |
return $q->whereIn('become', ['1']); | |
} | |
public function scopeAgency($q) | |
{ | |
return $q->whereIn('become', ['2']); | |
} | |
public function scopeActive($q) | |
{ | |
return $q->whereStatus('1'); | |
} | |
public function scopeReady($q) | |
{ | |
return $q->whereProfileUpdated('1'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment