I have a User
model with attributes like login, ID, age, etc. Another table is user_data
with a residence
column, for example.
How can I get all users with a specific residence? I have this:
User
model:
public function user_data()
{
return $this->hasMany('App\Models\UserData');
}
public function FilterUser($request)
{
if ($request->has('city'))
{
$users = User::with('user_data')->where('residence', 'Warsaw')->get();
}
dd($users);
}
UserData
model:
public function user()
{
return $this->belongsTo('App\User', 'user_id');
}
Now I get this error:
Column not found: 1054 Unknown column 'residence' in 'where clause' (SQL: select * from `users` where `residence` = warsaw and `users`.`deleted_at` is null)
And another question:
If User
is my main model, should I connect it with relationships like above? hasMany()
or belongsTo()
?