/home/mip/mip/app/Modules/Likod/Models/Clients/User.php
<?php

namespace QxCMS\Modules\Likod\Models\Clients;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Carbon\Carbon;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    protected $connection = 'qxcms';
    protected $table = "client_users";
    protected $guarded = ['password_confirmation'];
    protected $appends = ['hashid'];
    public $module_id = 3;

    /*
     * Model Accessors
     */

    public function getHashidAttribute()
    {
        return hashid($this->id);
    }

    public function setValidityDateAttribute($value) 
    {
        if($value <> '') {
            $dateformats = getDateFormatConfig('set');
            return $this->attributes['validity_date'] = Carbon::createFromFormat($dateformats['php'], $value)->format('Y-m-d');
        }
        return $this->attributes['validity_date'] = '0000-00-00';
    }

    public function getValidityDateAttribute($value) 
    {
        if($value <> '0000-00-00') {
            $dateformats = getDateFormatConfig('get');
            return Carbon::createFromFormat('Y-m-d', $value)->format($dateformats['php']);
        }
        return;
    }

    public function formValidityDateAttribute($value)
    {
        if($value <> '0000-00-00') {
            $dateformats = getDateFormatConfig('form');
            return Carbon::createFromFormat('Y-m-d', $value)->format($dateformats['php']);
        }
        return;
    }


    /*
     * Model Mutators
     */
    public function client()
    {
        return $this->belongsTo('QxCMS\Modules\Likod\Models\Clients\Client', 'client_id');
    } 
   
    public function role()
    {
        return $this->belongsTo('QxCMS\Modules\Client\Models\Settings\Roles\Role', 'role_id');
    }

    public function permissions()
    {
        return $this->hasMany('QxCMS\Modules\Client\Models\Settings\Roles\Permission', 'role_id', 'role_id');
    }

    public function post()
    {
        return $this->hasMany('QxCMS\Modules\Client\Models\Posts\Posts', 'author_id');
    } 

    public function userLogs()
    {
        return $this->hasMany('QxCMS\Modules\Client\Models\Settings\UserLogs\UserLogs', 'user_id');
    } 

    public function scopeFilterUsersByRole($query, $roleIds = array())
    {
        if(!empty($roleIds))
        {
            if(is_array($roleIds)) return $query->whereNotIn('role_id', $roleIds);
            return $query->where('role_id', $roleIds);
        }

        return $query;
    }

    public function scopeIsNotSubUser($query)
    {
        return $query->where('is_sub_user', 'No');
    }

    public function scopeIsSubUser($query)
    {
        return $query->where('is_sub_user', 'Yes');
    }

    public function page()
    {
        return $this->hasMany('QxCMS\Modules\Client\Models\Pages\Pages', 'author_id');
    }

    public function scopeSearchByName($query, $criteria = null)
    {
        if(!empty($criteria))
        {
            return $query->where('name', 'LIKE', '%'.$criteria.'%');
        }
    }

    public function scopeFieldOfficer($query)
    {
        return $query->where('access_type', config('role.field_officer'));
    }

    public function scopeEditor($query)
    {
        return $query->where('access_type', config('role.editor'));
    }

    public function scopeSearchUserType($query, $user_type = '')
    {
        if(!empty($user_type))
        {
            if(strtolower($user_type) == 'field_officer') return $query->fieldOfficer();
            if(strtolower($user_type) == 'editor') return $query->editor();
        }
        return $query;
    }

    public function scopeClientUser($query, $client_id = '')
    {
        if(auth()->check()){
            $client_id = auth()->user()->client->id;
        }
        return $query->where('client_id', $client_id);
    }
}