/home/mip/mip/app/Modules/Client/Models/Principals/Principal.php
<?php

namespace QxCMS\Modules\Client\Models\Principals;

/*use Illuminate\Database\Eloquent\Model;*/
use Illuminate\Foundation\Auth\User as Authenticatable;
use Carbon\Carbon;

class Principal extends Authenticatable
{
    protected $connection = "client";
    protected $table = "principals";
    
    public $module_id = 4;
    
    protected $guarded = []; 

    public $principal_status = [
        'Active' =>'Active',
        'Inactive' => 'Inactive'
    ];

    protected $appends = ['hashid'];

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

    public function setPasswordAttribute($value)
    {
        if(!empty($value)) {
            return $this->attributes['password'] = bcrypt($value);
        }
      	return $this->attributes['password'] = "";
    }

    public function setContactDetailsAttribute($value)
    {   
        if(is_array($value)) {
            $arr_clean = array_filter($value, 'strlen');
            if(!empty($arr_clean))
            {
                $str_value =  '"'.implode('","', $arr_clean).'"';
                return $this->attributes['contact_details'] = $str_value;
            }
            
        }

        return $this->attributes['contact_details'] = "";

    }

    public function getContactDetailsAttribute($value)
    {
        $clean_value = trim($value, '"');
        if(!empty($clean_value))
        {
           return $arr_value =  explode('","', $clean_value);
        }
        return '';
    }

    public function client()
    {
        return $this->belongsTo('QxCMS\Modules\Likod\Models\Clients\Client', 'client_id');
    }

    public function contacts()
    {
        return $this->hasMany('QxCMS\Modules\Client\Models\Principals\ContactPerson', 'principal_id');
    }

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