
Got the ERROR: QueryException in Connection.php line 651: SQLSTATE: Cardinality violation: 1241 Operand should contain 3 column(s) (SQL: select * from `posts` where (select DATE_FORMAT(created_at,'%Y-%m-%d %H:%i:%s'), `post_id`, count(*) as cmt_count from `comments` where `comments`.`post_id` = `posts`.`id` and `comments`. >where(DB::raw('`comments`.`created_at` BETWEEN NOW()-INTERVAL 8 DAY AND NOW()')) after some digging with the suggestions above, I have solved the problem myself. It can be used to perform most database operations in your application and works perfectly with all of Laravel's supported database systems. $q->select(DB::raw("DATE_FORMAT(created_at,'%Y-%m-%d %H:%i:%s')"),'post_id',DB::raw('count(*) as cmt_count')) Laravel's database query builder provides a convenient, fluent interface to creating and running database queries. And also use laravel select raw with multiple conditions in eloquent queries.
#Eloquent laravel raw query code#
Here is my code in Laravel Controller: $posts= Post::whereHas( You can use the laravel selectRaw eloquent method to building query in laravel apps.



Now I want to query for the post with the most number of comments from current date back to last 7 days.
