Ad

Find Record Either Belong The Main Table Or To The Relationship Eloquent ORM

- 1 answer

User table has first_name and the teacherSubjects have subject, i wan to put a condition in such a way if it matches to either the firstname in the User table or the subject in the teacherSubject

$teachers = User::with('teacherSubject')->whereHas('teacherSubject', function ($query) use($conditions){
                $query->where($conditions);
            })->get();
Ad

Answer

Try this way:

$teachers = User::with('teacherSubject')
            ->where(function($query) use($name) {
              $query->where('first_name',$name);
                    ->orWhereHas('teacherSubject', function ($query1) use($name){
                        $query1->where('subject',$name);
                    });
            })
            ->get();
Ad
source: stackoverflow.com
Ad