Passed
Push — master ( 6dc4e1...643de0 )
by Davide
31:15
created

Teacher::user()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace DavideCasiraghi\LaravelEventsCalendar\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
class Teacher extends Model
8
{
9
    /***************************************************************************/
10
    /**
11
     * The table associated with the model.
12
     *
13
     * @var string
14
     */
15
    protected $table = 'teachers';
16
17
    /***************************************************************************/
18
19
    protected $fillable = [
20
        'name', 'bio', 'country_id', 'year_starting_practice', 'year_starting_teach', 'significant_teachers', 'profile_picture', 'website', 'facebook', 'created_by', 'slug',
21
    ];
22
    
23
    /***************************************************************************/
24
    
25
    /**
26
     * Get the user that owns the event. eg. $teacher->user
27
     */
28
    public function user()
29
    {
30
        return $this->belongsTo('\Illuminate\Foundation\Auth\User', 'created_by');
31
    }
32
33
    /***************************************************************************/
34
    
35
    /**
36
     * Get the events for the teacher.
37
     */
38 3
    public function events()
39
    {
40 3
        return $this->belongsToMany('DavideCasiraghi\LaravelEventsCalendar\Models\Event', 'event_has_teachers', 'teacher_id', 'event_id');
41
    }
42
43
    /***************************************************************************/
44
45
    /**
46
     * Get the events where this teacher is going to teach to.
47
     *
48
     * @param  \DavideCasiraghi\LaravelEventsCalendar\Models\Teacher  $teacher
49
     * @return \Illuminate\Http\Response
50
     */
51 3
    public static function eventsByTeacher($teacher, $lastestEventsRepetitionsQuery)
52
    {
53 3
        $ret = $teacher->events()
54 3
                         ->select('events.title', 'events.category_id', 'events.slug', 'event_venues.name AS venue_name', 'countries.name AS country', 'event_venues.city AS city', 'events.sc_teachers_names', 'event_repetitions.start_repeat', 'event_repetitions.end_repeat')
55 3
                         ->join('event_venues', 'event_venues.id', '=', 'events.venue_id')
56 3
                         ->join('countries', 'countries.id', '=', 'event_venues.country_id')
57
                         ->joinSub($lastestEventsRepetitionsQuery, 'event_repetitions', function ($join) {
58 3
                             $join->on('events.id', '=', 'event_repetitions.event_id');
59 3
                         })
60 3
                         ->orderBy('event_repetitions.start_repeat', 'asc')
61 3
                         ->get();
62
63 3
        return $ret;
64
    }
65
}
66