Are you sure that $this->society is always 'A' or 'B' and there are no other values like 'a', 'b' or null?
Eloquent multiple relationship with condition
Hi all, first, sorry for my bad english.
Since two days, I bug on this problem at my work.
I have a mySql DB with 4 tables (old tables with no primary key, but it's not the problem).
My tables :
- Quiz
- User
- Program_society_A
- Program_society_B
Every User have 1 Quiz, and every User have 1 Program (but on society_A or society_B with the condition in a field "society" in User table)
So, in the controller, I want all the Quiz and I want do relation with user and program.
I do that in controller :
$quizList = QUIZ::with('user.program')->get();
and in the quiz model :
public function user()
{
return $this->belongsTo('App\Models\User');
}
and in the user model :
public function program()
{
if ($this->society == 'A') {
return $this->hasOne('\App\Models\A\Program');
} elseif ($this->society == 'B') {
return $this->hasOne('\App\Models\B\Program');
}
}
but it seems that society does not exist.
$thisis null with the relation
And when we look in the debug bar, Laravel do 2 queries separately (one for the relation with inscrit and one for program relation).
I hope you understand what I mean.
Please or to participate in this conversation.