<?php

namespace App\Models\Base;

use Illuminate\Database\Eloquent\Model;

use App\Models\Service;

abstract class ServiceType extends Model
{
	public $timestamps = FALSE;
	public $dateFormat = 'U';

	/**
	 * @NOTE: The service_id column could be discarded, if the id column=service_id
	 * @return \Illuminate\Database\Eloquent\Relations\MorphOne
	 */
	public function service()
	{
		return $this->morphOne(Service::class,'type','model','id');
	}

	/** SCOPES */

	/**
	 * Search for a record
	 *
	 * @param        $query
	 * @param string $term
	 * @return
	 */
	public function scopeSearch($query,string $term)
	{
		return $query
			->with(['service'])
			->join('ab_service','ab_service.id','=',$this->getTable().'.service_id')
			->Where('ab_service.id','like','%'.$term.'%');
	}

	/** ATTRIBUTES **/

	public function getTypeAttribute()
	{
		return strtolower((new \ReflectionClass($this))->getShortName());
	}
}