@rodrigo.pedra Allright I got that working :). I read more about the eloquent relationships and I built my models like I made my relations.
So with my EER in mind I made these models:
Student.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
class Student extends User
{
protected $table= 'users';
protected $guard_name = 'web';
public function getForeignKey()
{
return 'user_id';
}
protected static function boot()
{
parent::boot();
static::addGlobalScope('student', function (Builder $builder) {
$builder->whereExists(function ($query) {
$query->select(DB::raw(1))
->from('enrollments')
->whereRaw('enrollments.user_id = users.id');
});
});
}
public function enrollments(){
return $this->hasMany( Enrollment::class );
}
public function offerings(){
return $this->belongsToMany(Offering::class,'enrollments');
}
}
Enrollment.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Enrollment extends Model
{
public function student(){
return $this->belongsTo(Student::class,'user_id');
}
public function offering(){
return $this->belongsTo(Offering::class);
}
}
Offering.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Offering extends Model
{
public function schoolClasses(){
return $this->belongsTo(SchoolClass::class);
}
public function schoolyears(){
return $this->belongsTo(Schoolyear::class);
}
public function enrollments(){
return $this->hasMany(Enrollment::class);
}
public function students(){
return $this->belongsToMany(Student::class,'enrollments');
}
}
Schoolyear.php
<?php
namespace App;
class Schoolyear extends Model
{
public $timestamps =false;
public function offerings(){
return $this->hasMany(Offering::class);
}
}
SchoolClass.php
<?php
namespace App;
class SchoolClass extends Model
{
public $timestamps=false;
public function offering(){
return $this->hasMany(Offering::class);
}
public function grade(){
return $this->belongsTo(Grade::class);
}
}
Grades.php
<?php
namespace App;
class Grade extends Model
{
public $timestamps=false;
public function grades(){
return $this->hasMany(SchoolClass::class);
}
}
I think these relationships do follow my EER, or am I wrong?
Ideally I would like to do some queries now like:
-fetching all students for this year (school_id) with their class name + grade.
-Additionally I would also like to fetch all students from a certain class within a school year.
So far I tried these queries, but none with succes:
Fetching all students with grade name and class name for all years.
$students = Student::with('offerings.schoolClasses.grades')->get();
this does return the offering with the class_id and schoolyear_id but I cant get any further, like getting the grade and name of the class and fetching the schoolyear.
This I tried to filter on schoolyear id, but didn't work.
$students = Student::with(['offerings.schoolClasses.grades'=>function($query){
$query->where('schoolyear.id',2);
}])->get();
I already want to thank you for all your help and things that I learned from you :)