Ich habe zwei Tabellen survey
und question
in einem Laravel-Projekt. Ich möchte eine eloquente hasManyThrough-Beziehung erstellen, damit ich alle Fragen, die zur Umfrage gehören, durchlaufen kann.Laravel hasManyThrough - nicht alle erwarteten Ergebnisse
Erhebung Tabelle:
----------------
| id | title |
----------------
| 1 | fruit |
----------------
Frage Tabelle:
----------------
| id | title |
----------------
| 1 | apple? |
| 3 | banana? |
| 4 | kiwi? |
| 5 | pear? |
----------------
SurveyQuestion Tabelle:
--------------------------------
| id | survey_id | question_id |
--------------------------------
| 1 | 1 | 1 |
| 1 | 1 | 4 |
| 1 | 1 | 5 |
--------------------------------
In meiner Umfrage Modell Im Moment habe ich die folgende
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...
);
}
und in meinem SurveyQuestion Modell ich habe:
public function survey()
{
return $this->belongsTo(Survey::class);
}
public function question()
{
return $this->belongsTo(Questions::class);
}
Jedoch, wenn ich Schleife durch $survey->questions
es nur kehrt die Zeile, wenn question_id 1? Was habe ich falsch gemacht?
Ihre Umfrage und Fragetabelle enthalten keine Beziehung. Überprüfen Sie, dass: https://laravel.com/docs/5.5/eloquent-relationships#has-many-through –
@MahfuzShishir Dank der Verwendung von vielen zu vielen Beziehung vorgeschlagen von Morteza gelöst mein Problem. – xylar