| Server IP : 65.108.144.40 / Your IP : 216.73.217.165 Web Server : Apache/2.4.52 (Ubuntu) System : Linux ubuntu-8gb-hel1-1 5.15.0-173-generic #183-Ubuntu SMP Fri Mar 6 13:29:34 UTC 2026 x86_64 User : dev ( 1000) PHP Version : 8.2.30 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /var/www/html/project-slim/app/Models/ |
Upload File : |
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Carbon\Carbon;
abstract class BaseModel extends Model
{
/**
* The attributes that should be hidden for arrays.
* All ID fields are hidden by default for security
*/
protected $hidden = [
'id',
'user_id',
'company_id',
'role_id',
'report_id',
'module_id'
];
/**
* The attributes that should be cast to native types.
*/
protected $casts = [
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
/**
* Get the date format for the database.
*/
public function getDateFormat(): string
{
return 'Y-m-d H:i:s.u'; // PostgreSQL timestamp format
}
/**
* Generate a public UUID for external use instead of exposing internal IDs
*/
public function getPublicIdAttribute(): string
{
$tableName = $this->getTable();
$primaryKey = $this->getKeyName();
$primaryValue = $this->getAttribute($primaryKey);
// Generate a consistent hash-based UUID for this record
$data = $tableName . '_' . $primaryValue . '_' . config('app.key', 'default_secret');
return substr(hash('sha256', $data), 0, 32);
}
/**
* Convert attributes to array with custom formatting and ID protection
*/
public function toArray(): array
{
$array = parent::toArray();
// Add public_id instead of internal IDs
$array['public_id'] = $this->public_id;
// Format timestamps
if (isset($array['created_at'])) {
$array['created_at'] = $this->created_at?->toISOString();
}
if (isset($array['updated_at'])) {
$array['updated_at'] = $this->updated_at?->toISOString();
}
return $array;
}
/**
* Find model by public ID
*/
public static function findByPublicId(string $publicId): ?static
{
return static::all()->first(function ($model) use ($publicId) {
return $model->public_id === $publicId;
});
}
/**
* Scope for active records
*/
public function scopeActive($query)
{
return $query->where('is_active', true);
}
/**
* Scope for recent records
*/
public function scopeRecent($query, int $days = 30)
{
return $query->where('created_at', '>=', Carbon::now()->subDays($days));
}
/**
* Get formatted created date
*/
public function getFormattedCreatedAtAttribute(): string
{
return $this->created_at?->format('Y-m-d H:i:s') ?? '';
}
/**
* Get formatted updated date
*/
public function getFormattedUpdatedAtAttribute(): string
{
return $this->updated_at?->format('Y-m-d H:i:s') ?? '';
}
/**
* Boot the model
*/
protected static function boot()
{
parent::boot();
// Automatically set timestamps
static::creating(function ($model) {
if (empty($model->created_at)) {
$model->created_at = Carbon::now();
}
if (empty($model->updated_at)) {
$model->updated_at = Carbon::now();
}
});
static::updating(function ($model) {
$model->updated_at = Carbon::now();
});
}
}