Это отношение, найденное в запросе модель. < /p>
Код: Выделить всё
public function getQuestions(){
return $this->belongsToMany(Question::class)->withPivot('sub_test_id', 'test_id', 'score', 'answer');
}
< /code>
Это отношение, найденное в модели вопросов < /p>
public function getQueries(){
return $this->belongsToMany(Query::class)->withPivot('sub_test_id', 'test_id', 'score', 'answer');
}
< /code>
Это моя схема миграции многих ко многим < /p>
Schema::create('query_question', function (Blueprint $table) {
$table->id();
$table->bigInteger('question_id')->unsigned();
$table->bigInteger('query_id')->unsigned();
$table->bigInteger('sub_test_id')->unsigned();
$table->bigInteger('test_id')->unsigned();
$table->foreign('question_id')->references('id')->on('questions')
->onDelete('cascade')
->onUpdate('cascade');
$table->foreign('query_id')->references('id')->on('queries')
->onDelete('cascade')
->onUpdate('cascade');
$table->foreign('sub_test_id')->references('id')->on('sub_tests')
->onDelete('cascade')
->onUpdate('cascade');
$table->foreign('test_id')->references('id')->on('tests')
->onDelete('cascade')
->onUpdate('cascade');
$table->unique(['question_id', 'query_id']);
$table->double('score');
$table->string('answer');
});
Код: Выделить всё
$answers = array();
foreach ($query->getQuestions as $answer) {
array_push($answers, $answer->pivot);
}
< /code>
Здесь вы можете увидеть dd ($ Query);
dd ($ Query) < /p>
Здесь то же самое в тексте < /p>
App\Query {#1380 ▼
#table: "queries"
#fillable: array:2 [▶]
+timestamps: true
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: true
#attributes: array:5 [▼
"patient_id" => "2"
"query_type" => "dd"
"updated_at" => "2021-09-06 16:31:21"
"created_at" => "2021-09-06 16:31:21"
"id" => 4
]
#original: array:5 [▶]
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▼
"getQuestions" => Illuminate\Database\Eloquent\Collection {#1381 ▼
#items: []
}
]
#touches: []
#hidden: []
#visible: []
#guarded: array:1 [▼
0 => "*"
]
}
Подробнее здесь: https://stackoverflow.com/questions/690 ... in-laravel
Мобильная версия