glastree_on_gitea
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
<?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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user