Home > front end >  How to get user multiple column in Laravel Relationship?
How to get user multiple column in Laravel Relationship?

Time:10-28

I am using Laravel 9, I stuck into the Relationship, I have a table called test_series_question another is test_series_responses. Every test_series_question has one row in test_series_responses. I need to select the responses available in the table. Something like WHERE question_id = '$id' AND test_id = $test_id

I have tried to to get the responses from Laravel Model

function response()
{
   return $this->hasOne(TestSeriesResponse::class, 'question_id', 'id');
}

It's working fine, But There is a one column named test_id and if I dont compare test_id It'll return previously submitted responses which match the question_id id not the test_id

I have two questions

  1. Can I put a parameter into function response() in Model from controller?
  2. How can I achive the thing, Please help me out.

Here is the Controller code:

function testOngoing($test_id, $history_id)
{
  $test = TestSeries::find($test_id);
  $history = $history_id;
  $questions = TestSeriesQuestion::where('test_id', $test_id)->with('response')->paginate(1);
  $data = compact('test', 'questions', 'history');
  return view('test-series/test-question')->with($data);
}

Updated Modes

CodePudding user response:

Maybe something like

$questions = TestSeriesQuestion::with(['response' => function ($query) use ($history_id) {
    $query->where('history_id', $history_id);
}])->where('test_id', $test_id)->paginate(1)

  • Related