Search code examples
laravellaravel-5laravel-5.3laravel-5.4

How to join tables in laravel 5.4


In my case, I have 3 tables like Question, options, and answers

Questions table

|id      |     question_name|
------------------------------
      1          question1
      2          question2 
      3          question3

options table

id  |     question_id     |   options |
----------------------------------------
1           1                   option1
----------------------------------------
1           1                   option2
----------------------------------------
1           1                   option3
----------------------------------------
1           1                   option4

Answers table

id   |   customer_id |   question_id | answer(selected by user) |
--------------------------------------------------------------------
 1               1              1               option1          
--------------------------------------------------------------------
 1               2              2               option2            
--------------------------------------------------------------------
 1               1              3               option3    
--------------------------------------------------------------------
 1               1              3               option2           

How can I get below output from answers using joins table
For customer 1

 question1
    --option1
 question2
    --option2
 question3
    --option3
    --option2

I have eloquent relation,

 Question model 
    class Question extends Model
    {
        public function options()
        {
            return $this->hasMany(Option::class);
        }
        public function customer()
        {
            return $this->belongsTo(CustomerProfile::class);
        }
        public function answers()
       {
            return $this->hasMany(Answer::class);
       }
    }

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

   Answer model 
      public function customer()
      {
          return $this->belongsTo(CustomerProfile::class);
      }
      public function question()
      {
          return $this->belongsTo(Question::class);
      }

That is how my relationships looks like, Now I just need to join the tables to get output.


Solution

  • Going off the comment you left on Aaron Fahey's answer, you'll need to add a constraint to the query and the eager-load:

    $customerId = 1; 
    
    $questions = Question::with([
        'options', 'answers' => function ($query) use ($customerId) {
            $query->where('customer_id', $customerId);
        }])
        ->whereHas('answers', function ($query) use ($customerId) {
            $query->where('customer_id', $customerId);
        })
        ->get();
    

    https://laravel.com/docs/5.4/eloquent-relationships#constraining-eager-loads

    https://laravel.com/docs/5.4/eloquent-relationships#querying-relationship-existence

    Hope this helps!