如何连接 laravel 5.4 中的表
How to join tables in laravel 5.4
就我而言,我有 3 个 table,例如问题、选项和答案
问题table
|id | question_name|
------------------------------
1 question1
2 question2
3 question3
选项table
id | question_id | options |
----------------------------------------
1 1 option1
----------------------------------------
1 1 option2
----------------------------------------
1 1 option3
----------------------------------------
1 1 option4
答案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
如何使用连接 table
从答案中获得以下输出
对于客户 1
question1
--option1
question2
--option2
question3
--option3
--option2
我有eloquent关系,
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);
}
这就是我的关系的样子,现在我只需要加入 table 就可以获得输出。
为了获得与您的问题相关的选项,您可以使用eager loading。
例如:
$questions = Question::with('options')->get();
然后您将得到一个包含问题及其相关选项的集合。您将需要构建一个循环来获取您想要的数据。
我想,您还在客户模型 class 中定义了 eloquent 关系。
如果这样做,那么您可以通过客户模型检索特定客户的所有答案,这将为您提供获取答案的问题及其所有选项的钩子:
$customer = Customer::find($customerId);
$answer = $customer->answers()->where('id', $answerId)->get();
$question = $answer->question;
$questionOptions = $question->options;
希望对您有所帮助。
根据您在 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
希望对您有所帮助!
就我而言,我有 3 个 table,例如问题、选项和答案
问题table
|id | question_name|
------------------------------
1 question1
2 question2
3 question3
选项table
id | question_id | options |
----------------------------------------
1 1 option1
----------------------------------------
1 1 option2
----------------------------------------
1 1 option3
----------------------------------------
1 1 option4
答案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
如何使用连接 table
从答案中获得以下输出
对于客户 1
question1
--option1
question2
--option2
question3
--option3
--option2
我有eloquent关系,
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);
}
这就是我的关系的样子,现在我只需要加入 table 就可以获得输出。
为了获得与您的问题相关的选项,您可以使用eager loading。
例如:
$questions = Question::with('options')->get();
然后您将得到一个包含问题及其相关选项的集合。您将需要构建一个循环来获取您想要的数据。
我想,您还在客户模型 class 中定义了 eloquent 关系。 如果这样做,那么您可以通过客户模型检索特定客户的所有答案,这将为您提供获取答案的问题及其所有选项的钩子:
$customer = Customer::find($customerId);
$answer = $customer->answers()->where('id', $answerId)->get();
$question = $answer->question;
$questionOptions = $question->options;
希望对您有所帮助。
根据您在 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
希望对您有所帮助!