Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 hasManyThrough Pivot Table

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;
}

More Info

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');
});
like image 970
Michael Jones Avatar asked Sep 28 '22 12:09

Michael Jones


1 Answers

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).

like image 168
Khalil Laleh Avatar answered Oct 03 '22 17:10

Khalil Laleh