I am trying to create a relationship to access a table called comments through a model called grade, loaded through the students in the grade
Both the Grade and Student models belongToMany of the other
From my understanding, it is not possible to access a hasManyThrough relationship that requires a pivot table (comments do not have a Grade identifier, only a student identifier)
class Grade extends Model{
public function comments(){
return $this->hasManyThrough("App\Comment","App\Student");
}
}
I have these functions I found for Laravel 4 @ HasManyThrough with one-to-many relationship but it gives me the error Class 'App\Illuminate\Database\Eloquent\Relations\HasMany' not found
I don't have a good understanding on Namespaces, and can't work out what I should be doing in it's place for Laravel 5.
public function getCommentsAttribute()
{
if ( ! array_key_exists('comments', $this->relations)) $this->loadComments();
return $this->getRelation('comments');
}
protected function loadComments()
{
$comments = Comment::join('grade_student', 'comments.student_id', '=', 'grade_student.student_id')
->where('grade_student.grade_id', $this->getKey())
->distinct()
->get(['comments.*','grade_id']);
$hasMany = new Illuminate\Database\Eloquent\Relations\HasMany(Translation::query(), $this, 'grade_id', 'id');
$hasMany->matchMany(array($this), $comments, 'comments');
return $this;
}
Comment Table
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->integer('student_id')->unsigned();
$table->integer('domain_id')->unsigned();
$table->integer('teacher_id')->unsigned();
$table->text('comment');
$table->foreign('student_id')->references('id')->on('students') ->onDelete('cascade');
$table->foreign('domain_id') ->references('id')->on('domains')->onDelete('cascade');
$table->foreign('teacher_id')->references('id')->on('teachers')->onDelete('cascade');
});
My Students Table
Schema::create('students', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('unique_identifier');
$table->string('first_name');
$table->string('last_name');
$table->enum('gender',array('m','f'));
});
My Grades Table
Schema::create('grades', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('name',20);
$table->integer('school_id')->unsigned();
$table->integer('level_id')->unsigned();
$table->foreign('school_id')->references('id')->on('schools')->onDelete('cascade');
$table->foreign('level_id')->references('id')->on('classes_levels')->onDelete('cascade');
});
My Pivot Table
Schema::create('grade_student', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->timestamps();
$table->integer('grade_id')->unsigned();
$table->integer('student_id')->unsigned();
$table->integer('school_id')->unsigned();
$table->integer('year');
$table->foreign('grade_id')->references('id')->on('grades')->onDelete('cascade');
$table->foreign('student_id') ->references('id')->on('students')->onDelete('cascade');
$table->foreign('school_id')->references('id')->on('schools') ->onDelete('cascade');
});
Take a look at here.
At the moment Laravel 5.3 doesn't support hasManyThrough using pivot table. Just use an alternative ways.
As an alternative way:
Just consider these models and their relations:
A <=> B with pivot table A_B
B <=> C with pivot table B_C
Now you can create a pivot VIEW call A_C:
SELECT A_id, C_id FROM A_B
INNER JOIN B_C on A_B.B_id = B_C.B_id
GROUP BY A_id, C_id
I think you can guess my trick.
Yes, forget about hasManyThrough.
Now you can use A.belongsToMany(C).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With