I have orders table , I want to get orders that have been created at this month only (delivery process happens at the same day).
I want to compare between the carbon date which refers to this current month with the created_at field of type timestamps for orders.
This is my attempt:
$month = Carbon::today();
$currentMont = $month->month;
$thisMonthOrders = Order::where('place_id',$id)->where('stage',9)->whereDate('created_at',$currentMont)->get();
dd($thisMonthOrders);
The output that it gives me is an empty array.
You'll need to use the MONTH function in MySQL, along with a whereRaw:
$thisMonthOrders = Order::where('place_id',$id)
->where('stage',9)
->whereRaw('MONTH(created_at) = ?',[$currentMont])
->get();
However, you'll have issues when you have multiple years, unless you also add in a YEAR check. You might have better luck with whereBetween instead.
$start = Carbon::now()->startOfMonth();
$end = $start->copy()->endOfMonth();
$thisMonthOrders = Order::where('place_id',$id)
->where('stage',9)
->whereBetween('created_at',[$start, $end])
->get();
Related
I am working on a booking system and need to select only the records where a certain field is for today. Not today and in the future, but only today.
The query currently is:
$advancedBookings = Booking::where('type', 2)
->whereNull('estimated_booking_time')
->where('requested_booking_time', ?????);
$advancedBookings->get();
The requested_booking_time is the field that I wish to be checked, the date stored is in this format:
2017-08-23 08:00:00
So I want to only select rows that are on the same day as the current day.
As i understand, you want that records which is created today; then just get the today's date.
$today = date("Y-m-d");
now your query is like this.
$advancedBookings = Booking::where('type', 2)
->whereNull('estimated_booking_time')
->where('requested_booking_time', $today)->get();
You can use whereDate and format your dates with Carbon:
$advancedBookings = Booking::where('type', 2)
->whereNull('estimated_booking_time')
->whereDate('requested_booking_time', '=' , Carbon::today()->toDateString());
$advancedBookings->get();
Or format the your date before the query with Carbon.
You should try this:
$advancedBookings = Booking::where('type', 2)
->whereNull('estimated_booking_time')
->whereDate('requested_booking_time', '=', date('Y-m-d'))
->get();
I am new to laravel, I am building a small application which alerts when event date is a week away from current date. Here is my controller code
public function index()
{
$domain_count = domain_details::get()->count();
$domain_alert = domain_details::
where('domain_ex_date','>',date('Y-m-d'))
->get();
}
The domain_ex_date is in the format (YYYY-mm-dd) stored with the help of Carbon. The line where('domain_ex_date','>',date('Y-m-d')) gets me whole record when the domain_ex_date is away from the current date. i.e 2017-06-12 > 2016-09-15 gets the whole record. Here what i want to do is , i want to filter and get the only records which is only a week away from the current date. How do i do this ? i have tried like subweek() and subdays() but nothing helped.
I should get the record only when it satisfies this condition domain_ex_date - current date = 7
You can use strtotime():
domain_details:: where('domain_ex_date','<',date('Y-m-d',strtotime("+7 days")))
-> where('domain_ex_date','>',date('Y-m-d'))
->get();
Use Carbon!
Carbon is a build in date-extension ... Try it! :)
$week = Carbon::now()->addWeek();
$now = Carbon::now();
domain_details::where("domain_ex_date","<" $week)
->where("domain_ex_date", ">" $now)
->get()
Or you could also use the addDays($days) method!
$week = Carbon::now()->addDays(7);
I used carbon and this is what worked well for me
$week = Carbon::now()->subWeek();
$now = Carbon::now();
$domain_count = domain_details::get()->count();
$domain_alert = domain_details::where("domain_ex_date",">", $week)
->where("domain_ex_date", "<" ,$now)
->get();
I'm trying to use 'whereBetween' eloqouent query with two given start and end dates.
$first_day_this_month = date('Y-m-01 H:s:i'); //get the first day of the current month
$yesterDay = date('Y-m-d H:s:i',strtotime("-1 days")); //get yesterday's date
$d = m_chat_history::where('employee_id',$request->other_id)
->whereNull('to_group')->where('to_employee_id',$request->id)
->whereBetween('created_at',[$yesterDay,$first_day_this_month])
->get();
I make sure I have all the required data for the query by 'var_dump' and it did gives me all the required data needed for the query but the query returns me an empty output. Any ideas, clues, suggestions, help, recommendations please? I tried to remove the 'whereBetween' and my query works like it returns me the expected output but with 'whereBetween', the return output is empty.
Make sure to use the same type and format as created_at when defining the values for whereBetween. As you're using datetime you could define edge values like (just one of many ways of doing it):
$first_day_this_month = date('Y-m-01 H:i:s');
$yesterday = date('Y-m-d H:i:s', strtotime("-1 day"));
Also make sure of the order of params (still please consider edge cases like first day of the month where $yesterday would be smaller, so you have to add some logic and be careful):
->whereBetween('created_at', [$first_day_this_month, $yesterday])
Edit: wasn't timestamp...
Try this,
but first you have to install Carbon with composer.
after doing that
use Carbon
then write the fallowing code
$yesterday = Carbon::yesterday()->toDateTimeString();
$carbon = new Carbon('first day of ' . date('F Y'));
$first_day = $carbon->toDateTimeString();
and in your query
->whereBetween('created_at', [$first_day, $yesterday])
I'm having a tracker in Laravel 4 and I was wondering, how can I get the 'most' from my table.
So want I want is to have the day that had the most visitors.
My table structure looks like this:
So, my controller looks like this:
public function index()
{
$begin = date('Y-m-01');
$end = date('Y-m-t');
$visits = Tracker::selectRaw('date, count(ip)')->groupBy('date')->whereRaw("date between '$begin' and '$end'")->get();
//get total visits per month
$get_visits = Visitor::whereRaw("date between '$begin' and '$end'")->count();
// get average visits
// transform dates to DateTime objects (we need the number of days between $begin and $end)
$begin = new \DateTime($begin);
$end = new \DateTime('now');
$diff = $end->diff($begin); // creates a DateInterval object
$days = (int)$diff->format('%a'); // %a --> days
$average_visits = $get_visits / $days;
return View::make('admin.home.index')->with('stats', $visits)
->with('get_visits', $get_visits)
->with('average_visits', $average_visits);
}
What I want as an output:
We had the most visitors on 18/06/2015 (542 visitors)
For example.
Thanks!
You can SELECT the date ORDER BY the amount of visitors DESCENDING and LIMIT by 1. This is basic mysql. How to use this in laravel, is in the documentation.
Order by desc:
->orderBy('visits', 'desc')
Limit:
->take(1)
My example query is based on a system that has a column which counts the visits(which your structure doesn't have). So let me help you with your structure.
You want to have it by date so you basically GROUP BY date:
->groupBy('date')
From this query you want to have the amount of rows that belong to that date. So you use COUNT:
->count()
This is all based on that you know stuff about queries. This is all according to the query builder.
I want to query data from my inventories table where created_at date = today date
How do I do that in Laravel ? I tried
date_default_timezone_set ('America/New_York');
$today = date("d"); // 18 ( since today is 2/18/2015 )
$inventories = Inventory::where( 'created_at' ,'=', $today )->get();
I got 0 result.
You can't just compare the day of the month with a timestamp. Try this instead:
$today = Carbon::now()->toDateString();
$inventories = Inventory::whereRaw('DATE(created_at) = ?', [$today])->get();
Background info: Carbon is a DateTime library that Laravel uses. It makes things a bit easier but of course you could get the current date with date() as well. However date("d") will not return the current date but actually just the day of the month. The full date would be date('Y-m-d').