Skip to content

Instantly share code, notes, and snippets.

@makoru-hikage
Created December 3, 2016 21:11
Show Gist options
  • Save makoru-hikage/309cd9419db7bf29c554862065ce3c43 to your computer and use it in GitHub Desktop.
Save makoru-hikage/309cd9419db7bf29c554862065ce3c43 to your computer and use it in GitHub Desktop.
"Denote School Year.sql"'s Eloquent implementation
<?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