add projet
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\BaseModel;
|
||||
|
||||
class Role extends BaseModel
|
||||
{
|
||||
protected $table = 'roles';
|
||||
protected $primaryKey = 'id';
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'slug',
|
||||
'description',
|
||||
'dashboard_route',
|
||||
'priority',
|
||||
'is_active',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
];
|
||||
public $timestamps = true;
|
||||
const CREATED_AT = 'created_at';
|
||||
const UPDATED_AT = 'updated_at';
|
||||
|
||||
public function findByNamesOrSlugs(array $keys): array
|
||||
{
|
||||
if (empty($keys)) return [];
|
||||
|
||||
// Normalize for case-insensitive matching
|
||||
$lower = array_map('strtolower', $keys);
|
||||
|
||||
// We’ll match on lower(name) or lower(slug)
|
||||
return $this->where('is_active', 1)
|
||||
->groupStart()
|
||||
->whereIn('LOWER(name)', $lower)
|
||||
->orWhereIn('LOWER(slug)', $lower)
|
||||
->groupEnd()
|
||||
->orderBy('priority', 'ASC') // highest priority first
|
||||
->findAll();
|
||||
}
|
||||
|
||||
public function getRouteByNameOrSlug(string $key): ?string
|
||||
{
|
||||
$row = $this->where('is_active', 1)
|
||||
->groupStart()
|
||||
->where('LOWER(name)', strtolower($key))
|
||||
->orWhere('LOWER(slug)', strtolower($key))
|
||||
->groupEnd()
|
||||
->first();
|
||||
|
||||
return $row['dashboard_route'] ?? null;
|
||||
}
|
||||
|
||||
public function getRoles()
|
||||
{
|
||||
return $this->findAll();
|
||||
}
|
||||
|
||||
/** Map role names -> role IDs */
|
||||
public function getIdsByNames(array $names): array
|
||||
{
|
||||
$names = array_values(array_filter(array_map('strval', $names)));
|
||||
if (empty($names)) return [];
|
||||
|
||||
// collation is usually case-insensitive; if not, add LOWER() both sides
|
||||
$ids = $this->select('id')->whereIn('name', $names)->findColumn('id');
|
||||
return array_map('intval', $ids ?? []);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user