lundi 25 septembre 2017

Laravel hasManyThrough - not returning all expected results

I have two tables survey and question in a Laravel project. I want to create a hasManyThrough eloquent relationship so I can loop through all questions that have belong to the survey.

Survey Table:

----------------
| id | title   |
----------------
| 1  | fruit   |
----------------

Question Table:

----------------
| id | title   |
----------------
| 1  | apple?  |
| 3  | banana? |
| 4  | kiwi?   |
| 5  | pear?   |
----------------

SurveyQuestion table:

--------------------------------
| id | survey_id | question_id |
--------------------------------
| 1  | 1         | 1           |
| 1  | 1         | 4           |
| 1  | 1         | 5           |
--------------------------------

In my Survey Model I currently have the following

public function questions()
{
    return $this->hasManyThrough(
        Questions::class,
        SurveyQuestion::class,
        'question_id', // Foreign key on surveyquestion table...
        'id', // Foreign key on questions table...
        'id', // Local key on survey table...
        'survey_id' // Local key on surveyquestion table...
    );
}

and in my SurveyQuestion model I have:

public function survey()
{
  return $this->belongsTo(Survey::class);
}

public function question()
{
  return $this->belongsTo(Questions::class);
}

However when I loop through $survey->questions it is only returning the row when question_id is 1? What's have I done wrong?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2yokY17
via IFTTT

Aucun commentaire:

Enregistrer un commentaire