I would like to load posts from users of a certain age from the database. The user can save the minimum(users->profile->min_age) and maximum age(users->profile->max_age) in his profile.
The age for the logged-in user I receive with the following:
Auth::user()->birthday->diff(Carbon::now())->format('%y');
However, every time I need the age of the user who has created the post. I can not get the age in the same query ... I'm confused
this is my current query, but without age:
Post::where('type', 7)->whereIn('gender', [0,1])->where('user_id', '!=' , Auth::user()->id)
->whereNotExists(function ($query) {
$query->select(DB::raw(1))
->from('users_finder')
->whereColumn('users_finder.post_id', 'posts.id')
->where('users_finder.user_id', auth()->id());
})->first();
Post.php
public function user() {
return $this->belongsTo('App\User', 'user_id');
}
User.php
public function posts()
{
return $this->hasMany('App\Post');
}
public function profile()
{
return $this->hasOne('App\Profile');
}
profile.php
public function users()
{
return $this->belongsTo('App\User');
}
Does that work with where or do I have to use a join for this? How can I load the corresponding posts?