-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCourse.php
96 lines (72 loc) · 2.1 KB
/
Course.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Course extends Model
{
// use soft delete instead of permanent delete
use SoftDeletes;
use HasFactory;
protected $perPage = 2;
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'courses';
protected $fillable = ['students_id', 'course_id',];
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['deleted_at'];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
];
/**
* Load all for admin and paginate
*
* @return Paginator
*/
public static function loadAll(): Paginator
{
return static::latest()
->paginate();
}
public function tutor(): BelongsTo
{
return $this->belongsTo(User::class, 'tutor_id');
}
public function enrollments(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(Enrollment::class, 'course_id');
}
public static function getAvailableCourses(string $language = null, string $type = null): ?\Illuminate\Contracts\Pagination\LengthAwarePaginator
{
$query = self::query()->where('deleted_at', null);
if ($language) {
$query->where('language', $language);
}
if ($type) {
$query->where('type', $type);
}
$now = now();
// DB 와 서버 모두 UTC
return $query->where('available_from', '<=', $now)
->where('available_until', '>=', $now)->latest()->paginate();
}
public static function getAvailableOne(int $id = null)
{
$now = now();
return self::query()->where('available_from', '<=', $now)
->where('available_until', '>=', $now)->find($id);
}
}