So I have events table which has start date and end date.
What I want to do is a comparison between today and start date + end date so if today is between start and end, return that collection which will be displayed on page, if it isn't ignore that event.
Problem is that when I retrieve an event I cannot access it as it is return that it doesn't exist in the collection, but it does after view is returned.
Here's my controller:
public function show($id)
{
$today = date("Y-m-d");
$today_dt = new DateTime($today);
$event = Event::with('businesses')
->get();
$test = $event->startdate;
$test2 = $event->enddate;
//something like if $today is not less that start date and not higher than end date, return that collection?
dd($test);
return view('events.showEvent', compact('event'));
}
use where date function like this
$today = Carbon::now();
$event = Event::with('businesses')
->whereDate('startdate', '<', $today->format('Y-m-d'))
->whereDate('enddate', '>', $today->format('Y-m-d'))
->get();
If I understood your problem correctly, I think this should suffice:
$today = Carbon::today();
$event = Event::whereDate('startdate', '>', $today->format('Y-m-d'))
->whereDate('enddate', '<', $today->format('Y-m-d'))
->with('businesses')
->get();
I hope you did search the internet for this problem in the first place
in Model
public function scopeOfToday($query){
$today = \Carbon\Carbon::today()->format('Y-m-d');
return $query->whereRaw("? BETWEEN startdate and enddate",$today);
}
in Controller
public function show($id)
{
$event = Event::ofToday()->with('businesses')->get();
$test = $event->startdate;
$test2 = $event->enddate;
//something like if $today is not less that start date and not higher than end date, return that collection?
dd($test);
return view('events.showEvent', compact('event'));
}
Related
Consider:
$tglA = $request->tglA;
$tglB = $request->tglB;
$group = $request->group;
$barangId = $request->barangId;
$kodeMerk = $request->kodeMerk;
$interval = DateInterval::createFromDateString('1 day');
$tglTotal = new DatePeriod($tglA, $interval, $tglB);
if($group == 'true') {
if(empty($request->departemenId)) {
$scoreboard = HistoryPenjualan::select('kodeSales', 'departemenId', DB::raw('SUM(score) as totalScore'))
->whereBetween('tgl', [$tglA, $tglB])
->groupBy('departemenId')
->orderBy('totalScore', 'desc')
->get();
}
How can I sum the score value of the same date of whereBetween date range and then not show the duplicated date?
You can try to GROUP BY DATE(tgl):
$scoreboard = HistoryPenjualan::select('kodeSales', 'departemenId', DB::raw('SUM(score) as totalScore'))
->whereBetween('tgl', [$tglA, $tglB])
->groupBy('departemenId', DB::raw('DATE(tgl)'))
->orderBy('totalScore', 'desc')
->get();
I've got a query in my Laravel project that returns a collection of data from my database between two dates, it's then grouped by the hour (but I can change this to my liking), I'd now like to return the "dates" for data before they existed in the database as a way of building up a list of dates that I can display in my web page, eve though there will be no data for these days, my chart would look consistent.
As an example:
I want to see the past 30 days of data as a history, but the first 20 days of this period have no data yet, I still need to return the date keys for these days, except with just no data in them.
My current code is as follows (I'm using Carbon):
$uptimeData = UptimeChecks::where('user_id', 1)
->where('monitor_id', 1)
->where('checked_at', '>=', '2021-01-01 00:00:00')
->where('checked_at', '<=', '2021-01-30 23:59:59')
->orderBy('checked_at', 'asc')
->select('event', 'checked_at')
->get();
$from = Carbon::now()->subDays(60);
$period = CarbonPeriod::create($from, '2021-01-30 23:59:59');
$dates = $period->toArray();
foreach ($dates as $key => $date) {
$dates[$key] = Carbon::parse($date)->format('Y-m-d');
}
$uptimeData = collect($uptimeData);
$uptimeData = $uptimeData->merge($dates);
$uptimeDataTimeline = $uptimeData->groupBy(function ($item, $key) {
if (isset($item->checked_at)) {
$date = Carbon::parse($item->checked_at);
} else {
$date = Carbon::parse($item);
}
return $date->format('Y-m-d');
});
Even though there would be no entries to display a "checked_at" column, can this be spoofed with the date for that day with no data?
Update 03/05 # 20:30
I've updated my description to reflect the latest attempt to solve this problem, I appear to have constructed what I need, however, have some issues:
Where I'm looping over my $dates is there a way to build up some structure within each item so I don't have to do if/else checks on all my variables and keys?
For some reason, the "fake" dummy dates are being added after the real data, e.g: 1st Jan, 2nd Jan, 30th Dec ... how can I reverse this?
One way to achieve this is by creating a collection of the dates for the time period you want, and then merging the results of a query grouped by date into it.
Here's an example:
$from = '2021-01-01';
$to = '2021-01-31';
$period = CarbonPeriod::create($from, $to);
$dates = collect($period->toArray())->mapWithKeys(function ($date) {
return [$date->format('Y-m-d') => []];
});
// $dates = ['2021-01-01' => [], '2021-01-02' => [], ...etc]
$uptimeChecks = UptimeChecks::query()
->where('user_id', 1)
->where('monitor_id', 1)
->whereBetween('checked_at', [$from, $to])
->orderBy('checked_at', 'asc')
->select('event', 'checked_at')
->get();
$uptimeDates = $uptimeChecks->groupBy(function ($item, $key) {
return $item->checked_at->format('Y-m-d');
});
// $uptimeDates = ['2021-01-02' => ['event1', 'event2'], ...etc]
$uptimeData = $dates->merge($uptimeDates);
// $uptimeData = ['2021-01-01' => [], '2021-01-02' => ['event1', 'event2'], ...etc]
This assumes you are casting the checked_at field to a date in your UptimeChecks Model.
I want to get some data from database table, where date field is in range between two dates.
Here is my code below:
$date = new \DateTime('today');
$newDate = new \DateTime('today');
$newDate->add(new \DateInterval('P2D'));
$query = $repository->createQueryBuilder('s')
->select('s.day')
->where('s.day > :data')
->andWhere('s.day < :newDate')
->setParameter('data', $date)
->setParameter('newDate', $newDate)
->getQuery();
$dates = $query->getResult();
But unfortunately it doesn't work.
The second method gives empty array, too.
$date = new \DateTime('today');
$newDate = new \DateTime('today');
$newDate->add(new \DateInterval('P2D'));
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery(
'SELECT s.day
FROM AppBundle:Seance s
WHERE s.day BETWEEN :date AND :newDate')
->setParameter('date', $date)
->setParameter('newDate', $newDate);
$seances= $query->getResult();
If I remove andWhere clause or between, doctrine returns data correctyly but all recordse
Does anyone have idea why it doesn't work?
In my application I do what you are asking, using the following:
/* #var $date1 \DateTime */
$date1 = \DateTime::createFromFormat('d/m/Y', $this->request()->get("from_date"));
$date1->format('Y-m-d h:i:s');
$date1->setTime(0, 0, 0); // You'll likely not need this bit if times don't matter to you
/* #var $date2 \DateTime */
$date2 = \DateTime::createFromFormat('d/m/Y', $this->request()->get("to_date"));
$date2->format('Y-m-d h:i:s');
$date2->setTime(23, 59, 59); // You'll likely not need this bit if times don't matter to you
Then in it's repository we do this:
->andWhere("s.date >= :date1 AND s.date < :date2)
You simply needed to pass correct criteria.
$dateFrom = (new \DateTime())->setTime(0, 0, 0);
$dateTo = (new \DateTime())->add(new \DateInterval('P2D'))->setTime(23, 59, 59);
$query = $repository->createQueryBuilder('s')
->where('s.day >= :dateFrom', 's.day <= :dateTo')
->setParameter('dateFrom', $dateFrom)
->setParameter('dateTo', $dateTo)
->getQuery();
$dates = $query->getResult();
I want to display in the admin panel statitics about users and other things in the database.
For example to show how many users were registered today, this month etc.
For now I am doing this with the users for today the following way:
$users = User::where('admin', 0)->get();
$usersRegisteredToday = array_filter($users->toArray(), function ($user) {
$registerDate = \DateTime::createFromFormat('Y-m-d H:i:s', $user['created_at']);
$registerDate->setTime(0,0,0);
$today = new \DateTime();
$today->setTime(0,0,0);
$diff = $today->diff($registerDate);
$diffDays = (integer)$diff->format("%R%a"); // Extract days count in interval
return $diffDays == 0;
});
return view('admin.index', compact("users", "usersRegisteredToday"));
And in the view:
<p class="text-no">Today: {{ count($usersRegisteredToday) }} </p>
I wonder if there is a better, simpler and faster way to do this, because I think if I get the information for the other things that way it will be very slow and heavy. So i want to know the best and lightest way to do this.
As of Laravel 5.3 we can use whereDate / whereMonth / whereDay / whereYear
For example to get records created today:
$users = User::whereDate('created_at', DB::raw('CURDATE()'))->get();
Possibly a similar question is asked here: Get only records created today in laravel
$todayStart = (new Carbon())->setTime(0, 0);
$todayEnd = (new Carbon())->setTime(23, 59, 59);
$users = User::where('admin', 0)
->where('created_at', '>=', $todayStart)
->where('created_at', '<=', $todayEnd)
->get();
You could use query scopes which would make it even better. Inside User model:
class User extends Model
{
public function scopeCreatedBefore(Carbon $date)
{
return $this->where('created_at', '<=', $date);
}
public function scopeCreatedAfter(Carbon $date)
{
return $this->where('created_at', '>=', $date);
}
}
And for the usage:
$todayStart = (new Carbon())->setTime(0, 0);
$todayEnd = (new Carbon())->setTime(23, 59, 59);
$users = User::where('admin', 0)
->createdAfter($todayStart)
->createdBefore($todayEnd)
->get();
Its quite easy to do it. Im doing this in laravel 5.6
We are working in controller here. Lets say $date1 and $date2 is your range.
First, parse the date to carbon
$olddate = Carbon::parse($date1)->format('Y-m-d');
$newdate = Carbon::parse($date2)->format('Y-m-d');
Now, we get the users within that date range.
$users = Users::latest()
->whereBetween('created_at', array($olddate, $newdate))
->orderBy('id', 'desc')
->get(); // or count if you just want to count
basically, the function will look like below
public function betweendates(Request $request)
{
// get dates from request
$date1= $request->olddate;
$date2= $request->newdate;
// parse date to carbon
$olddate = Carbon::parse($date1)->format('Y-m-d');
$newdate = Carbon::parse($date2)->format('Y-m-d');
// the magic stuff
$users = Users::latest()
->whereBetween('created_at', array($olddate, $newdate))
->orderBy('id', 'desc')
->count();
return view('users.dashboard', compact('users'));
}
I have fields in my table for date, but they contain everything - day, year and month. Can I write a query to get only the records, which has month equal to the current month? I can do this:
$today = new \DateTime();
$month = $today->format('m');
$cat = $em->getRepository('EMBudgetTrackerBundle:Expense')->find(1);
$ex_date = $cat->getDate();
and compare $month and $ex_date, but can I write some kind of query? Something like this:
public function getExpensesByMonth($month)
{
$q = $this->createQueryBuilder('e');
$q->select('e')
->where('e.date = :date')
->setParameter('date', $month);
return $q->getQuery()->getResult();
}
Thank you in advance! :)
If you database column is in DateTime format you can use the DateTime object in your query. As far as I know you can only query for time ranges though.
public function getExpensesByMonth($beginning, $end)
{
$q = $this->createQueryBuilder('e');
$q->select('e')
->where('e.date > :beginning')
->andWhere('e.date < :end')
->setParameter('beginning', $beginning)
->setParameter('end', $end);
return $q->getQuery()->getResult();
}