In my my laravel 5.7.3 application , I need to get grouped date like
$voteItemUsersResults = VoteItemUsersResult::orderBy('created_at', 'desc')
->groupBy('created_at')
->having('created_at', '>', '2018-09-01')
->get();
but I need to get only day part of created_at, without time.
Which is the valid way to make it ?
Grouped by Only Day of Datetime .
$voteItemUsersResults = VoteItemUsersResult::orderBy('created_at', 'desc')
->groupBy(DB::raw('DATE_FORMAT(created_at, "%d")'))
->having('created_at', '>', '2018-09-01')
->get();
Related
I'm trying to call 2 columns from 2 different tables. applicants.ic_no and agents.ic_no. It have different values.
Using the following codes only displayed only ic_no from agents.ic_no
$claimLists = ClaimDetail::join('applicants', 'applicants.ic_no', '=', 'claim_details.ic_no')
->join('agents', 'agents.id', '=', 'claim_details.agent_id')
->where('claim_date', $cutt_off)
->groupBy('agents.id', 'claim_details.id', 'applicants.id')
->orderBy('agents.id')
->orderby('amount', 'desc')
->get();
How do i get both columns to display?
This is because, you have ic_no in both tables. By default MYSQL picks one of it to display, it is the same as having id in both tables and from your results, how would you know which table's ic_no you are accessing while they have the same name?
Alternatively you can use select and DB::raw to change the name of one of the ic_no fields, and similiarly for any other similiar fields. For example;
$claimLists = ClaimDetail::join('applicants', 'applicants.ic_no', '=', 'claim_details.ic_no')
->join('agents', 'agents.id', '=', 'claim_details.agent_id')
->select('claim_details.*', DB::raw('agents.ic_no as agents_ic_no'), 'agents.XXX', 'agents.YYYY', 'applicants.XXX')
->where('claim_date', $cutt_off)
->groupBy('agents.id', 'claim_details.id', 'applicants.id')
->orderBy('agents.id')
->orderby('amount', 'desc')
->get();
instead of XXX and YYY, you can put the fields that you would like to get and you can get as many as you want or remove them, if you don't want to get any field from the second table, but the main thing here is you are access the agents.ic_no as agents_ic_no or whatever name you would like to give it.
I solve this issue by adding select to rename the conflict column name. So this is the code:
$processed = ClaimDetail::join('applicants', 'applicants.ic_no', '=', 'claim_details.ic_no')
->join('agents', 'agents.id', '=', 'claim_details.agent_id')
->select('*')
->selectRaw('applicants.ic_no as nokp, agents.ic_no as agic')
->where('claim_details.agent_id',$agent->id)
->orderBy('claim_details.claimable', 'desc')
->orderby('claim_details.amount', 'desc')
->get();
Thanks guys for your time.
I have so far came up with this query:
Order::select(DB::raw('sum(price) as sums'),
DB::raw("DATE_FORMAT(created_at,'%e') as day")
)
->where('seller_id', Auth::user()->id)
->whereBetween('created_at', [Carbon::now()->startOfMonth()
->subMonth(),Carbon::now()->endOfMonth()->subMonth()])
->orderBy('created_at', 'asc')
->groupBy('day')
->get();
This returns an array like this:
[{"sums":145867,"day":"3"},{"sums":19567,"day":"28"}]
This is almost what I need. I would now like to include even the name of the month in these results, like this:
[{"June" : {{"sums":145867,"day":"3"},{"sums":19567,"day":"28"}}}]
Simple, add month to your select and use collection method mapToGroups
If you don't want to see month number in result array - format result array in callback
...
DB::raw("DATE_FORMAT(created_at,'%c') as month")
...
->get()
->mapToGroups(function ($item, $key) {
return [Carbon\Carbon::create()->month($item['month'])->format('M') => $item];
});
Also you can use DATE_FORMAT(created_at,'%b') for month name from MySQL and don't use Carbon in callback, but, in my opinion, it's not good for possible localization. But this way more performance effective
What's wrong with my Laravel query, it is giving one less result than original result.
Test::where('has_ftd', 1)
->where('ftd_date', '>=', '2018-10-17')
->where('ftd_date', '<=', '2018-10-17')
->paginate(10);
Update: I Just noticed that less than equal to '<=' is giving less than '<' result
When querying datetime or timestamp values, it's generally important to include the "time" portion to ensure you're querying the correct values:
Test::where('has_ftd', 1)
->where('ftd_date', '>=', '2018-10-17 00:00:00')
->where('ftd_date', '<=', '2018-10-17 23:59:59')
->paginate(10);
Providing the values 2018-10-17 00:00:00 and 2018-10-17 23:59:59 will ensure the query handles values for a full, inclusive date.
For ranges you can use Laravel whereBetween() method. you can try this:-
Test::where('has_ftd', 1)
->whereBetween('ftd_date', [$date_from, $date_to])
->paginate(10);
I think this will work.
Ref: https://laravel.com/docs/5.7/queries#where-clauses
I`m having trouble with an Eloquent query.
The structure of my Model is:
Coupon (for get discounts for a booking):
period_start (coupon is valid for some specific periods)
period_end (this end date, for example: the coupon is valid when you make a booking between 2018-02-01 and 2018-03-03)
booking_start (the period of when the booking is done)
booking_end (you can book for example from 2018-01-15 till 2018-01-30 only for the coupon to be valid)
Code:
$coupon = Coupon::where('period_start', '<=', $request->get('from_date'))
->where('period_end', '<=', $request->get('to_date'))
->where('booking_start', '<=', now()->format('Y-m-d'))
->where('booking_end', '>=', now()->format('Y-m-d'))
->first();
In the order field people pick two dates from a datepicker, thats where the request()'s are for.
This should work, but I don't get any results...
What about using dateBetween?
$start = Carbon::parse($request->start)->startOfDay(); //2016-09-29 00:00:00.000000
$end = Carbon::parse($request->end)->endOfDay(); //2016-09-29 23:59:59.000000
$clicks->dateBetween($start, $end);
https://laracasts.com/discuss/channels/eloquent/daterange-with-wherebetween
You should re format the date that get from the datepicker before query part, like this.
$date=date('Y-m-d', strtotime(str_replace('-', '/', $request->to_date)));
Following Laravel Query I Write for to get Upto Previous Date Records that's not getting any Records. If I Remove Date query its get Many Records.
$data['frmdate_submit'] format is 2017-05-24.
How to Fix this Problem
$getpreviousbalance = Companyledger::where('transaction_date', '>', $data['frmdate_submit'])
->WhereIn('frm_ledger', $ledgerlist)
->where('company_id', $companyids)
->get();
Use whereData instead of where when you are checking with dates.
$getpreviousbalance=Companyledger::whereDate('transaction_date','>',$data['frmdate_submit'])->WhereIn('frm_ledger',$ledgerlist)->where('company_id',$companyids)->get();
hope this will works.
$getpreviousbalance = Companyledger::where('company_id', $companyids)
->whereIn('frm_ledger', $ledgerlist)
->where('transaction_date', '>', \Carbon\Carbon::parse($data['frmdate_submit']))
->get();