I have two tables: usersand students. These tables are linked by fields:
users.id = studetns.user_id
I am trying to get information using the query:
$students = User::where(function ($query) use ($request) {
if (($term = $request->get("name"))) {
$query->where('name', 'like', '%' . $term . '%');
$query->where('secondname', 'like', '%' . $term . '%');
}
})->with("details")->where('type', '2')->orderBy('name', 'desc')->get();
dd($students);
You can see with("details")which should connect to the tables.
My model User:
public function details()
{
return $this->hasOne('App\Student', 'user_id', 'id');
}
When I execute the request, I get an error:
select * from `users` where `type` = 2 order by `name` desc)
This means that there was no join to the second table where there are namefiels.
source
share