File "StudentSubject.php"

Full Path: /home/trinadezambia/public_html/admin_panel/app/Models/StudentSubject.php
File size: 1.35 KB
MIME-type: text/x-php
Charset: utf-8

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use App\Traits\DateFormatTrait;
use App\Services\CachingService;

class StudentSubject extends Model
{
    use HasFactory;
    use DateFormatTrait;

    protected $fillable = [
        'student_id',
        'class_section_id',
        'class_subject_id',
        'session_year_id',
        'school_id'
    ];

    public function class_subject()
    {
        return $this->belongsTo(ClassSubject::class);
    }

    public function scopeOwner($query)
    {
        if (Auth::user()) {
            $sessionYearId = app(CachingService::class)->getSessionYear()->id;
            if (Auth::user()->hasRole("School Admin") || Auth::user()->hasRole("Teacher") || Auth::user()->hasRole("Student")) {
                return $query->where(['school_id' => Auth::user()->school_id, 'session_year_id' => $sessionYearId]);
            }
        }
        return $query;
    }

    public function getCreatedAtAttribute()
    {
        return $this->formatDateValue($this->getRawOriginal('created_at'));
    }

    public function getUpdatedAtAttribute()
    {
        return $this->formatDateValue($this->getRawOriginal('updated_at'));
    }

    public function student()
    {
        return $this->belongsTo(User::class);
    }
}