reconstruction of the project
This commit is contained in:
@@ -2,12 +2,14 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use App\Models\BaseModel;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class PromotionQueue extends BaseModel
|
||||
{
|
||||
protected $table = 'promotion_queue';
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
protected $fillable = [
|
||||
'student_id',
|
||||
'from_class_section_id',
|
||||
@@ -20,25 +22,97 @@ class PromotionQueue extends BaseModel
|
||||
'updated_at',
|
||||
'updated_by',
|
||||
];
|
||||
|
||||
public $timestamps = true;
|
||||
const CREATED_AT = 'created_at';
|
||||
const UPDATED_AT = 'updated_at';
|
||||
|
||||
public function upsert(array $data): bool
|
||||
protected $casts = [
|
||||
'student_id' => 'integer',
|
||||
'from_class_section_id' => 'integer',
|
||||
'to_class_id' => 'integer',
|
||||
'to_class_section_id' => 'integer',
|
||||
'updated_by' => 'integer',
|
||||
'created_at' => 'datetime',
|
||||
'updated_at' => 'datetime',
|
||||
];
|
||||
|
||||
/* ============================================================
|
||||
* Relationships (optional)
|
||||
* ============================================================
|
||||
*/
|
||||
|
||||
public function student(): BelongsTo
|
||||
{
|
||||
if (empty($data['student_id']) || empty($data['school_year_to'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$existing = $this->where('student_id', (int)$data['student_id'])
|
||||
->where('school_year_to', (string)$data['school_year_to'])
|
||||
->first();
|
||||
|
||||
if ($existing) {
|
||||
return (bool) $this->update((int)$existing[$this->primaryKey], array_merge($existing, $data));
|
||||
}
|
||||
|
||||
return (bool) $this->insert($data);
|
||||
return $this->belongsTo(Student::class, 'student_id');
|
||||
}
|
||||
}
|
||||
|
||||
public function fromClassSection(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(ClassSection::class, 'from_class_section_id');
|
||||
}
|
||||
|
||||
public function toClassSection(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(ClassSection::class, 'to_class_section_id');
|
||||
}
|
||||
|
||||
public function toClass(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(SchoolClass::class, 'to_class_id'); // rename model if yours is ClassModel/ClassRoom/etc.
|
||||
}
|
||||
|
||||
/* ============================================================
|
||||
* Scopes (reusable filters)
|
||||
* ============================================================
|
||||
*/
|
||||
|
||||
public function scopeForStudent(Builder $q, int $studentId): Builder
|
||||
{
|
||||
return $q->where('student_id', $studentId);
|
||||
}
|
||||
|
||||
public function scopeForSchoolYearTo(Builder $q, string $schoolYearTo): Builder
|
||||
{
|
||||
return $q->where('school_year_to', $schoolYearTo);
|
||||
}
|
||||
|
||||
public function scopeStatus(Builder $q, string $status): Builder
|
||||
{
|
||||
return $q->where('status', $status);
|
||||
}
|
||||
|
||||
/* ============================================================
|
||||
* Upsert (Laravel style)
|
||||
* ============================================================
|
||||
*/
|
||||
|
||||
/**
|
||||
* True upsert by (student_id, school_year_to).
|
||||
*
|
||||
* Returns the saved model, or null if required keys missing.
|
||||
*/
|
||||
public static function upsertQueue(array $data): ?self
|
||||
{
|
||||
$studentId = isset($data['student_id']) ? (int) $data['student_id'] : null;
|
||||
$yearTo = isset($data['school_year_to']) ? (string) $data['school_year_to'] : null;
|
||||
|
||||
if (!$studentId || !$yearTo) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Only update columns that are allowed.
|
||||
$payload = array_intersect_key($data, array_flip((new static)->getFillable()));
|
||||
|
||||
return static::updateOrCreate(
|
||||
['student_id' => $studentId, 'school_year_to' => $yearTo],
|
||||
$payload
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Optional: keep the same CI signature (bool result).
|
||||
*/
|
||||
public static function upsertBool(array $data): bool
|
||||
{
|
||||
return (bool) static::upsertQueue($data);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user