Created
December 3, 2016 21:11
-
-
Save makoru-hikage/309cd9419db7bf29c554862065ce3c43 to your computer and use it in GitHub Desktop.
"Denote School Year.sql"'s Eloquent implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace DeltaX\Models; | |
use Illuminate\Database\Eloquent\Model; | |
use Carbon\Carbon; | |
class SchoolCalendar extends Model { | |
protected $guarded = ['created_at', 'updated_at', 'is_deleted']; | |
protected $hidden = ['created_at', 'updated_at', 'is_deleted']; | |
protected $appends = ['academic_year']; | |
protected $table = 'school_calendar'; | |
public function sessionsDuringThis(){ | |
return $this->hasMany('\DeltaX\Models\CourseSession'); | |
} | |
public function getAcademicYearAttribute(){ | |
$year_start = Carbon::parse($this->date_start)->year; | |
$year_end = Carbon::parse($this->date_end)->year; | |
$academic_year = | |
self::selectRaw('CONCAT(YEAR(MIN(date_start)),"-",YEAR(MAX(date_end))) AS ay') | |
->whereRaw( | |
'YEAR(date_start) = ? AND (YEAR(date_end) BETWEEN ? AND ?)', | |
[$year_start, $year_end, $year_end + 1] | |
)->first()->ay; | |
return $academic_year; | |
} | |
public function getAcademicYearSemesterAttribute(){ | |
return $this->academic_year . ", " . $this->semester; ; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment