vendredi 13 septembre 2019

How can i Get Laravel relationship with its relationship values?

I have a laravel database design where every result has belongsToMany relationship with question and every question has belogsTo relationship with subject. i want to fetch subject names from results with single statement on laravel

I have tried this https://laravel.com/docs/5.6/eloquent-relationships#has-many-through from laravel documentation

here is my code from Result model

public function questions(){
    return $this->belongsToMany('App\questions', 'results_questions', 'result_id', 'questions_id');
}

the code from question model

public function subject(){
    return $this->belongsTo('App\Subject','subject_id');
}

and this is what i tried from documentation (doesnot work)

public function subjects(){
    return $this->hasManyThrough(
        'App\Subject',
        'App\questions',
        'subject_id', // Foreign key on question table...
        'questions_id', // Foreign key on result table...
        'id', // Local key on question table...
        'id' // Local key on result table...
    );
}**strong text**



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/31lxf5l
via IFTTT

Aucun commentaire:

Enregistrer un commentaire