0

Users have multiple questions and questions have multiple users.
Questions have multiple answers.
Users have multiple answers.
Relations are set in model.

tables:

questions --> id   question
answers --> id q_id user_id answer
users --> id email username
question_user --> user_id q_id

How can i retreive the answer given by particular user of the particular question.

this way i can get the answers and questions of the user:

$users=User::where(['id'=>1])->with('questions','answers')->get(); 
foreach($users as $user)
{
    foreach($user->questions as $question)
    {
        echo "<pre>";print_r($question->question);                      
    }

    foreach($user->answers as $answer)
    {
        echo "<pre>";print_r($answer->answer);          
    }
}

and this way i can get the questions of the user but all answers of the questions.

$users=User::where(['id'=>1])->with('questions','answers')->get(); 
        foreach($users as $user)
        {
            foreach($user->questions as $question)
            {
                echo "<pre>";print_r($question->question);  
                foreach($question->answers as $answer)
                {
                    echo "<pre>";print_r($answer->answer);
                }                   
            }
        }

But i am having the trouble of getting the question of the particular user and answer of the question by particular user. this way
Results i need

user id=1.
question 1
answer by user id 1.
question 2
answer by user id 1
...



 user id=2.
 question 1
 answer by user id 2.
 question 2
 answer by user id 2
   ...
Cœur
  • 37,241
  • 25
  • 195
  • 267
Steve
  • 1,622
  • 5
  • 21
  • 39
  • this is your question right check the answer[LINK] (http://stackoverflow.com/questions/39113682/insert-multiple-records-at-once-with-laravel/39396657?noredirect=1#comment66132997_39396657) – Hamelraj Sep 09 '16 at 12:35
  • I'm having troubles understanding what it is you want. You know the question and you know the user and you need to find the answer(s) given by the user to the question? – Matey Sep 09 '16 at 19:08

2 Answers2

1

try this

foreach($users as $user)
    {
        foreach($user->questions as $question)
        {
            echo "<pre>";print_r($question->question);  
            foreach($question->answers as $answer)
            {
                  echo "<pre>";print_r($answer->filter(function ($answer) use ($user) {
                    return $answer->user_id== $user->id;
                });
            }                   
        }
    }
user259947
  • 59
  • 6
1

there you go

// User model
public function answeredQuestions()
{
  return $this->belongsToMany(Question::class, 'answers', 'user_id', 'question_id');
}

// then
$user->answeredQuestions()->find($question_id)->pivot->answer;

// or to make it 'safe' - no Trying to get property on non-object if no answer is found
data_get($user->answeredQuestions()->find($question_id), 'pivot.answer');
Jarek Tkaczyk
  • 78,987
  • 25
  • 159
  • 157
  • i already has hasmany relation with answer and belongsToMany with questions is this yet another function? – Steve Sep 09 '16 at 14:11
  • @Steve yes, basically your `answers` table links `users` and `questions`, so we can use it as pivot table. You could achieve the same by `$user->answers()->where('question_id', $question_id)->first()` - that would be the answer to the question you're looking for. – Jarek Tkaczyk Sep 10 '16 at 00:04