61 lines
1.4 KiB
PHP
61 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Support\Str;
|
|
|
|
class Classe extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = ['etablissement_id', 'user_id', 'nom', 'formation', 'code_classe'];
|
|
|
|
protected $casts = [
|
|
'archivee_a' => 'datetime',
|
|
];
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (Classe $classe) {
|
|
$classe->code_classe ??= static::genererCodeUnique();
|
|
});
|
|
}
|
|
|
|
public static function genererCodeUnique(): string
|
|
{
|
|
do {
|
|
$code = Str::upper(Str::random(6));
|
|
} while (static::where('code_classe', $code)->exists());
|
|
|
|
return $code;
|
|
}
|
|
|
|
public function etablissement(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Etablissement::class);
|
|
}
|
|
|
|
public function prof(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'user_id');
|
|
}
|
|
|
|
public function eleves(): HasMany
|
|
{
|
|
return $this->hasMany(Eleve::class);
|
|
}
|
|
|
|
public function seances(): HasMany
|
|
{
|
|
return $this->hasMany(Seance::class);
|
|
}
|
|
|
|
public function estArchivee(): bool
|
|
{
|
|
return $this->archivee_a !== null;
|
|
}
|
|
}
|