I'm trying to display only data that has today's date in the created_at timestamp.
But I'm getting all my data instead of only today's data
Here is my code
$now = now();
$products = Product::selectRaw('status, created_at, date(created_at), hour(created_at), count(*) as total')
->whereRaw('created_at', $now)
->groupByRaw('status, date(created_at), hour(created_at), created_at')
->get();
$now = now();
$products = Product::selectRaw('status, created_at, date(created_at), hour(created_at), count(*) as total')
->whereDate('created_at', '=', date('Y-m-d'))
->groupByRaw('status, date(created_at), hour(created_at), created_at')
->get();
The following code should work.
$products = Product::selectRaw('status, created_at, date(created_at), hour(created_at), count(*) as total')
->whereDate('created_at', DB::raw('CURDATE()'))
->groupByRaw('status, date(created_at), hour(created_at), created_at')
->get();
Related
How to add a subquery to the main query with a condition? The result of the request should be similar to this:
select
`tasks`.*,
`users`.`name` as `user_name`,
(select count(*) from tasks_favorites on tasks_favorites.task_id = tasks.id and tasks_favorites.user_id = 38) as `is_favorite`
from `tasks`
left join `users` on `users`.`id` = `tasks`.`user_id`
where
`tasks`.`id` = 149
I try this query but I get an error:
$task = DB::table('tasks')
->select(
'tasks.*',
'users.name as user_name',
)
->when(Auth::check(), function($query) {
return $query->addSelect(
DB::table('tasks_favorites')->where('tasks_favorites.task_id', 'tasks.id')->where('tasks_favorites.user_id', auth()->user()->id)->count()
) ;
})
->leftJoin('users', 'users.id', 'tasks.user_id')
->where('tasks.id', $task_id)
->get()
->first() ;
did you try the selectRaw or raw method?
something like this
$task = DB::table('tasks')
->select(
'tasks.*',
'users.name as user_name',
)
->when(Auth::check(), function($query) {
return $query->addSelect(
DB::raw('select count(id) from tasks_favorites where tasks_favorites.task_id=tasks.id AND tasks_favorites.user_id='.auth()->user()->id.' as mycount')
);
})
->leftJoin('users', 'users.id', 'tasks.user_id')
->where('tasks.id', $task_id)
->get()
->first() ;
I have start and end date time column in database
start_date end_date contact
2021-01-25 17:30:00 2021-01-25 20:30:00 xxxxxxxxx
2021-01-27 17:30:00 2021-01-28 19:30:00 xxxxxxxxx
Now i'm trying to get records which exists between these two date time, it should match mobile number also.I'm using below query but it is not filtering record
$date = date('Y-m-d H:i:s', strtotime($request->date));
$contact_number=$request->contact_number;
$event=Event::where(function ($query)use($date,$contact_number){
$query->where('start_date', '>=', $date)->where('end_date', '<=', $date);})->where('contact',$contact_number)->paginate(5);
You have to invert the >= and <= signs
$date = date('Y-m-d H:i:s', strtotime($request->date));
$contact_number = $request->contact_number;
$event = Event::where(function ($query) use ($date, $contact_number){
$query->where('start_date', '<=', $date)
->where('end_date', '>=', $date);
})
->where('contact',$contact_number)
->paginate(5);
Laravel 4+ offers you these methods: whereDay(), whereMonth(), whereYear() and whereDate() .
Here are some ways to make the comparison :
->where('date', '<=', '2014-07-10 23:59:59')
->where('date', '<', '2014-07-11')
// '2014-07-11'
$dayAfter = (new DateTime('2014-07-10'))->modify('+1 day')->format('Y-m-d');
->where('date', '<', $dayAfter)
Try this:
$date = Carbon\Carbon::parse($request->date)->format('Y-m-d H:i:s');
$contact_number=$request->contact_number;
$event=Event::where(function ($query)use($date,$contact_number){
$query->where('start_date', '<=', $date)->where('end_date', '>=', $date);})->where('contact',$contact_number)->paginate(5);
I have DB query like this :
$visitor = DB::table('visitor_anonymous')
->join('users', 'visitor_anonymous.host', '=', 'users.host')
->whereBetween('visitor_anonymous.created_at', [$startDate, $endDate])
->select(DB::raw('DATE(visitor_anonymous.created_at) AS date, COUNT(visitor_anonymous.host) AS visitor'))
->groupBy(DB::raw("DATE(visitor_anonymous.created_at)"));
$session = DB::table('visitor_anonymous')
->join('visitor_anonymous_pageviews', 'visitor_anonymous.session_id', '=', 'visitor_anonymous_pageviews.session_id')
->join('users', 'visitor_anonymous.host', '=', 'users.host')
->whereBetween('visitor_anonymous_pageviews.created_at', [$startDate, $endDate])
->select(DB::raw('DATE(visitor_anonymous_pageviews.created_at) AS date, COUNT(visitor_anonymous_pageviews.session_id) AS session'))
->groupBy(DB::raw("DATE(visitor_anonymous_pageviews.created_at), visitor_anonymous.session_id"));
$pageview = DB::table('visitor_anonymous')
->join('visitor_anonymous_pageviews', 'visitor_anonymous.session_id', '=', 'visitor_anonymous_pageviews.session_id')
->join('users', 'visitor_anonymous.host', '=', 'users.host')
->whereBetween('visitor_anonymous_pageviews.created_at', [$startDate, $endDate])
->select(DB::raw('DATE(visitor_anonymous_pageviews.created_at) AS date, sum(visitor_anonymous_pageviews.count) AS pageview'))
->groupBy(DB::raw("DATE(visitor_anonymous_pageviews.created_at)"));
Hope result like this but didn't work. How to join subquery like this ?
$table = DB::table( DB::raw("({$visitor->toSql()}) as v"))
->leftJoin(DB::raw("({$session->toSql()}) as s"), 'v.date','=','s.date')
->leftJoin(DB::raw("({$pageview->toSql()}) as p"), 'v.date','=','p.date')
->select(DB::raw('v.date, sum(visitor) AS visitor, sum(session) AS session, sum(pageview) AS pageview'))
->groupBy(DB::raw("v.date"))
->mergeBindings($visitor);
Please tell me, solution. Thanks
I have
$dt = Carbon::now();
$b4 = Carbon::now()->addWeeks(12);
Event::orderBy('date', 'desc')
->where('date', '<', $b4)
->where('ends', '>=', $dt)
->orWhere('date', '>=', $dt)
->take(3)
->get();
All filters work except the first where. I tried it following the orWhere first, no dice. It displays items with the date in August 2018. Help?
I think you need to group your where queries ie.
$dt = Carbon::now();
$b4 = Carbon::now()->addWeeks(12);
Event::orderBy('date', 'desc')
->where(function ($query) use ($dt, $b4) {
$query->where('date', '<', $b4->format('Y-m-d'))
->where('ends', '>=', $dt->format('Y-m-d'));
})
->orWhere('date', '>=', $dt->format('Y-m-d'))
->take(3)
->get();
https://laravel.com/docs/5.5/queries#parameter-grouping
I'm creating a timeline and I'm nearly finished. I want that the color of the date for each timeline event is the same beside if the date is "today".
So I need something like:
#if($event[$i]->created_at->format('d.m.Y') == *code or variable that says its today*)
....
#endif
But I couldn't figure out what I can do to save the todays date in a variable.. Does anybody knows a solution for this?
thanks!
You can use isToday() method to check if date is today:
if ($event[$i]->created_at->isToday())
you can use Carbon
#if($event[$i]->created_at->format('d.m.Y') == \Carbon::today() )
....
#endif
You can use
whereDate, whereMonth, whereDay, whereYear, whereTime
whereDate()
$users = DB::table('users')
->whereDate('created_at', '2019-11-31')
->get();
whereMonth()
$users = DB::table('users')
->whereMonth('created_at', '10')
->get();
whereDay()
$users = DB::table('users')
->whereDay('created_at', '20')
->get();
whereYear()
$users = DB::table('users')
->whereYear('created_at', '2019')
->get();
whereTime()
$users = DB::table('users')
->whereTime('created_at', '=', '11:20:45')
->get();