I'm new to programming world and I use laravel. I have have Post
model, every user can have more posts. For for all posts I have hasMany
relation but this is related to posts, and I need inverse logic.
I don't know how can I get only users which last post is older then 30 days? I need them for email notification.
Can somebody give me some inputs please?
CodePudding user response:
https://laravel.com/docs/8.x/eloquent-relationships#querying-relationship-existence
So it should be something like this:
User::whereHas('posts', function ($query) {
return $query->where('created_at', '<=', now()->subDays(30);
})->get()
CodePudding user response:
You can use Carbon subDays()
like below:
$data = User::whereHas('posts',function(Builder $query){
$query->('created_at', '<=', Carbon::now()->subDays(30)->toDateTimeString())
})->get();