I have 3 models. User / Post / Category
Following is my model code, models are simple.
// User class
class User extends Model
{
public function posts(){
return $this->hasMany( Post::class);
}
}
// Post class
class Post extends Model
{
$fillable = ['reg_date', ...];
function user(){
return $this->belongsTo( User::class);
}
function category(){
return $this->belongsTo( Category::class);
}
}
// Category class
class Category extends Model
{
public function posts(){
return $this->hasMany( Post::class);
}
}
Post has 'reg_date' field
I want to get users list who wrote posts at 'reg_date' = '2022-10-01' more than 2 times, and the the category_id = 9.
How can I make this query ?
I guess the query should be something like this.
$users = User::query()
->whereHas(['posts', function($q){
$q->where('post_cat_id', 9);
$q->where('reg_date', '2022-10-01');
;;
}])
->get();
Somebody can help me ?