114 lines
2.7 KiB
PHP
114 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Exception;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class BaseModel extends Model
|
|
{
|
|
|
|
private static $removeAppends = [];
|
|
private static $addAppends = [];
|
|
private static $addWith = [];
|
|
|
|
public function removeAppends ( array $appends )
|
|
{
|
|
$appends = is_array($appends)?$appends:[];
|
|
self::$removeAppends = $appends;
|
|
}
|
|
|
|
public function addAppends ( array $appends )
|
|
{
|
|
$appends = is_array($appends)?$appends:[];
|
|
self::$addAppends = $appends;
|
|
}
|
|
|
|
/*public function addWith (array $with )
|
|
{
|
|
$with = is_array($with)?$with:[];
|
|
self::$addWith = $with;
|
|
}*/
|
|
|
|
private function _removeAppends ()
|
|
{
|
|
try
|
|
{
|
|
$origin = $this->appends;
|
|
if ( ! is_array ( $this->appends ))
|
|
{
|
|
return $origin;
|
|
}
|
|
|
|
if(isset(self::$removeAppends[0]) && self::$removeAppends[0] == "*")
|
|
{
|
|
$this->appends = [];
|
|
return $this->appends;
|
|
}
|
|
|
|
foreach ($this->appends as $key => $perAppend)
|
|
{
|
|
if (in_array ( $perAppend, self::$removeAppends ))
|
|
{
|
|
unset( $this->appends[ $key ] );
|
|
}
|
|
}
|
|
return $this->appends;
|
|
} catch ( Exception $e )
|
|
{
|
|
$message = $e->getFile()." ".$e->getLine()." ".$e->getMessage();
|
|
Log::error($message);
|
|
}
|
|
|
|
}
|
|
|
|
private function _addAppends()
|
|
{
|
|
try
|
|
{
|
|
foreach (self::$addAppends as $perAppend)
|
|
{
|
|
$methodMakeUp = "get".strtolower($perAppend)."attribute";
|
|
if(method_exists($this,$methodMakeUp))
|
|
{
|
|
$this->append($perAppend);
|
|
}
|
|
}
|
|
} catch ( Exception $e )
|
|
{
|
|
$message = $e->getFile()." ".$e->getLine()." ".$e->getMessage();
|
|
Log::error($message);
|
|
}
|
|
}
|
|
|
|
/*private function _addWith ()
|
|
{
|
|
foreach (self::$addWith as &$perWith)
|
|
{
|
|
try {
|
|
$this->load($perWith);
|
|
} catch (Exception $e) {
|
|
continue;
|
|
}
|
|
|
|
/*
|
|
if (method_exists($this,$perWith))
|
|
{
|
|
$this->load($perWith);
|
|
}
|
|
}
|
|
}*/
|
|
|
|
|
|
protected function getArrayableAppends()
|
|
{
|
|
/*$this->_addWith();*/
|
|
$this->_removeAppends();
|
|
$this->_addAppends();
|
|
return $this->appends;
|
|
}
|
|
|
|
}
|