55 lines
1.4 KiB
PHP
55 lines
1.4 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Models;
|
||
|
|
|
||
|
|
use Illuminate\Database\Eloquent\Model;
|
||
|
|
use Illuminate\Support\Facades\DB;
|
||
|
|
|
||
|
|
class Ruolo extends Model
|
||
|
|
{
|
||
|
|
protected $table = 'ruoli';
|
||
|
|
|
||
|
|
protected $fillable = ['nome', 'descrizione', 'ordine', 'attiva'];
|
||
|
|
|
||
|
|
protected $casts = [
|
||
|
|
'attiva' => 'boolean',
|
||
|
|
'ordine' => 'integer',
|
||
|
|
];
|
||
|
|
|
||
|
|
public static function attive(): \Illuminate\Database\Eloquent\Collection
|
||
|
|
{
|
||
|
|
return static::where('attiva', true)->orderBy('ordine')->get();
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function opzioni(): array
|
||
|
|
{
|
||
|
|
return static::attive()->pluck('nome', 'id')->toArray();
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function findByIds(?array $ids): \Illuminate\Database\Eloquent\Collection
|
||
|
|
{
|
||
|
|
if (empty($ids)) {
|
||
|
|
return new \Illuminate\Database\Eloquent\Collection();
|
||
|
|
}
|
||
|
|
return static::whereIn('id', $ids)->get();
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function namesByIds(?array $ids): string
|
||
|
|
{
|
||
|
|
if (empty($ids)) {
|
||
|
|
return '';
|
||
|
|
}
|
||
|
|
return static::whereIn('id', $ids)
|
||
|
|
->orderBy('ordine')
|
||
|
|
->pluck('nome')
|
||
|
|
->implode(', ');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function getMembriCount(): int
|
||
|
|
{
|
||
|
|
return DB::table('gruppo_individuo')
|
||
|
|
->whereNotNull('ruolo_ids')
|
||
|
|
->whereRaw("JSON_CONTAINS(ruolo_ids, ?)", [json_encode($this->id)])
|
||
|
|
->count();
|
||
|
|
}
|
||
|
|
}
|