mardi 29 mars 2016

Laravel 5.2 eager loading no working

Heres what i am doing.

$question = ForumQuestion::where('link',$ques)->with('answers')->first();
$answers = $question->answers;
$answers->load('user');
//return $answers;
return view('forum.question', compact('question','answers'));

the $answers->load('user'); eager loads corresponding user of the answer.

public function user()
    {
        if ($this->user_type == 'student') {
            return $this->belongsTo(Student::class, 'user_id');
        }else{
            return $this->belongsTo(Teacher::class, 'user_id');
        }
    }

But problem is $this->user_type gets some kind of static. If my first answer has user_type = 'teacher' then in every query it assumes as it is teacher even though it changes some time to student. Why it is static? If I don't eager load it works well.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire