Your IP : 216.73.216.79


Current Path : /var/www/html/redd/app/Models/Backend/
Upload File :
Current File : /var/www/html/redd/app/Models/Backend/Route.php

<?php

namespace App\Models\Backend;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Table;
use Illuminate\Database\Eloquent\SoftDeletes;

#[Table('routes')]
#[Fillable([
    'name',
    'uri',
    'method',
    'controller',
    'action',
    'middleware',
    'type',
    'status',
    'alias',
])]
class Route extends Model
{
    use HasFactory, SoftDeletes;

    protected function casts(): array
    {
        return [
            'status' => 'boolean',
        ];
    }

    public static function rules($id = null): array
    {
        $uniqueUri = $id ? "unique:routes,uri,$id,id" : 'unique:routes,uri';

        return [
            'name' => ['nullable', 'string', 'max:255'],
            'uri' => ['nullable', 'string', 'max:255', $uniqueUri],
            'method' => ['nullable', 'string', 'max:50', 'in:GET,POST,PUT,PATCH,DELETE,OPTIONS,HEAD'],
            'controller' => ['nullable', 'string', 'max:255'],
            'action' => ['nullable', 'string', 'max:255'],
            'middleware' => ['nullable', 'string', 'max:500'],
            'type' => ['nullable', 'string', 'max:20', 'in:web,api,admin'],
            'status' => ['nullable', 'boolean'],
            'alias' => ['nullable', 'string', 'max:255'],
        ];
    }

    public static function methodColors(): array
    {
        return [
            'GET' => 'bg-brand-50 text-brand-600 dark:bg-brand-500/15 dark:text-brand-500',
            'POST' => 'bg-success-50 text-success-600 dark:bg-success-500/15 dark:text-success-500',
            'PUT' => 'bg-warning-50 text-warning-600 dark:bg-warning-500/15 dark:text-orange-400',
            'PATCH' => 'bg-warning-50 text-warning-600 dark:bg-warning-500/15 dark:text-orange-400',
            'DELETE' => 'bg-error-50 text-error-600 dark:bg-error-500/15 dark:text-error-500',
            'OPTIONS' => 'bg-gray-50 text-gray-600 dark:bg-gray-500/15 dark:text-gray-500',
            'HEAD' => 'bg-brand-50 text-brand-600 dark:bg-brand-500/15 dark:text-brand-500',
        ];
    }

    public static function typeColors(): array
    {
        return [
            'web' => 'bg-brand-50 text-brand-600 dark:bg-brand-500/15 dark:text-brand-500',
            'api' => 'bg-warning-50 text-warning-600 dark:bg-warning-500/15 dark:text-orange-400',
            'admin' => 'bg-error-50 text-error-600 dark:bg-error-500/15 dark:text-error-500',
        ];
    }

    public static function statusColors(): array
    {
        return [
            'active' => 'bg-success-50 text-success-600 dark:bg-success-500/15 dark:text-success-500',
            'inactive' => 'bg-error-50 text-error-600 dark:bg-error-500/15 dark:text-error-500',
        ];
    }

    public function scopeWeb($query)
    {
        return $query->where('type', 'web');
    }

    public function scopeApi($query)
    {
        return $query->where('type', 'api');
    }

    public function scopeAdmin($query)
    {
        return $query->where('type', 'admin');
    }

    public function scopeActive($query)
    {
        return $query->where('status', true);
    }

    public function scopeInactive($query)
    {
        return $query->where('status', false);
    }
}