如何在laravel 5.4中连接表

时间:2017-07-29 06:55:13

标签: laravel laravel-5 laravel-5.2 laravel-5.3 laravel-5.4

就我而言,我有3个表,如问题,选项和答案

问题表

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

选项表

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

答案表

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           

如何使用联接表来获得低于答案的输出       对于客户1

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

我有雄辩的关系,

 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);
      }

这就是我的人际关系的样子,现在我只需要加入表来获得输出。

3 个答案:

答案 0 :(得分:1)

为了获得问题的相关选项,您可以使用eager loading

一个例子是:

$questions = Question::with('options')->get();

然后,您将拥有一个包含问题及其相关选项的集合。您需要构建一个循环来获取所需的数据。

答案 1 :(得分:0)

我想,您还在Customer Model类中定义了雄辩的关系。 如果您这样做,那么您可以通过客户模型检索特定客户的所有答案,这将使您能够获得答案的问题及其所有选项:

$customer = Customer::find($customerId);
$answer = $customer->answers()->where('id', $answerId)->get();

$question = $answer->question;

$questionOptions = $question->options;

我希望它会有所帮助。

答案 2 :(得分:0)

关于你在Aaron Fahey的回答中留下的评论,你需要为查询和急切加载添加一个约束:

$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

希望这有帮助!