Using brackets to specify two or more where clauses into its own where function in eloquent

Suppose we need to fetch records from post table where user id of post needs to be 1 and they are either created on the day of 22 or updated on the day  of 23 of any month and year, then we can achieve this fetching by following snippet.

$posts = Post::select([ 'id','title', 'created_at','updated_at'])->where( 'user_id', 1 )->where(function( $q ){ 
	return $q->whereDay('created_at', 22)->orWhereDay('updated_at',23);
})->get();


With above snippet it produces following raw sql where we can see date(created_at) or date(updated_at) are being put inside bracket with another 'and' condition of user_id.

select * from `posts` where `user_id` = ? and (date(`created_at`) = ? or date(`updated_at`) = ?)

Related Posts


Using WhereIn

Updating multiple rows

Using Like

Using orWhere

What is Query Builder?

Using "with" in eloquent query

Getting last inserted id

Selecting last row of a table

Using parameters in find() method

Using local scope in eloquent

Using global scope in eloquent