I'm trying to count how many tickets our operators have closed in a week(and per day) I have these 2 queries
$closedWeek = Ticket::join('activity_log', 'activity_log.rel_id', '=', 'ticket.id')
->where([
['activity_log.created_at', '>', $fri],
['activity_log.user_id', '=', $id],
['activity_log.event_name', '=', 'ticket_closed'],
['number', 'like', 'SD%']
])->count();
$closedWeek2 = Ticket::join('activity_log', 'activity_log.rel_id', '=', 'ticket.id')
->where([
['activity_log.created_at', '>', $fri],
['activity_log.user_id', '=', $id],
['number', 'like', 'SD%'],
['activity_log.event_name', '=', 'ticket_department_updated']
])
->where(function($query) {
$query->where('activity_log.new_value', '=', 'closed Uninvoiced')
->orWhere('activity_log.new_value', '=', 'To Invoice')
->orWhere('activity_log.new_value', '=', 'closed Other')
->orWhere('activity_log.new_value', '=', 'closed Invoiced');
})->count();
$closedWeek = $closedWeek + $closedWeek2;
They are bringing back slightly bigger numbers than expected and I think some of them are duplicated. How could I put these into one query using where(function($query)) or do I have to use DB::raw instead? I can then count unique ids
You can use GroupBy(); with id,
Example:
$closedWeek = Ticket::join('activity_log', 'activity_log.rel_id', '=', 'ticket.id')
->where([
['activity_log.created_at', '>', $fri],
['activity_log.user_id', '=', $id],
['activity_log.event_name', '=', 'ticket_closed'],
['number', 'like', 'SD%']
])
->groupBy('ticket.id')
->count();
$closedWeek2 = Ticket::join('activity_log', 'activity_log.rel_id', '=', 'ticket.id')
->where([
['activity_log.created_at', '>', $fri],
['activity_log.user_id', '=', $id],
['number', 'like', 'SD%'],
['activity_log.event_name', '=', 'ticket_department_updated']
])
->whereIn('activity_log.new_value',
[ 'closed Uninvoiced',
'To Invoice',
'closed Other',
'closed Invoiced',
])
->groupBy('ticket.id')
->count();
$closedWeek = $closedWeek + $closedWeek2;
OBS:
You dont need '=', on equal wheres, example, where('colun', '=', 'abc'); you can change to where('colun', 'abc');
Please test and see if you have the expected result.
References:
How to get distinct values for non-key column fields in Laravel?
Related
I have issue with my query. My sample query is as below
$answers = DB::table('IC2017_AY')
->join('IC2017', 'IC2017_AY.UNITID', '=', 'IC2017.UNITID')
->join('SFA1617_P1', 'IC2017_AY.UNITID', '=', 'SFA1617_P1.UNITID')
->join('cost', 'IC2017_AY.UNITID', '=', 'cost.UNITID')
->join('gpa', 'IC2017_AY.UNITID', '=', 'gpa.UNITID')
->join('enrollment', 'IC2017_AY.UNITID', '=', 'enrollment.UNITID')
->join('major', 'IC2017_AY.UNITID', '=', 'major.UNITID')
->join('preference', 'IC2017_AY.UNITID', '=', 'preference.UNITID')
->select('IC2017_AY.TUITION2 as in_state_tuition','IC2017_AY.FEE2 as in_state_fee','IC2017_AY.TUITION3 as out_state_tuition','IC2017_AY.FEE3 as out_state_fee','IC2017_AY.CHG4AY3 as books_supplies','IC2017_AY.CHG6AY3 as other_expenses','IC2017.ROOMAMT as room_charge','IC2017.BOARDAMT as board_charge','IC2017.RMBRDAMT as combined_charge','SFA1617_P1.SCUGRAD as total_receiving','SFA1617_P1.SCUGFFN as full_time_receiving','SFA1617_P1.UAGRNTN as total_receiving_grant')
->where($where_data);
->get();
but now I want to pass the array of join because joins will be decided at run time something like this
below code is just the example.
$joins = [
['country', 'user_master.country_id', '=', 'country.country_id'],
['city', 'user_master.city_id', '=', 'city.city_id'],
['login_master', 'user_master.user_id', '=',
'login_master.user_id'],
];
How can I do this in laravel 5.8
thanks in advance.!
You can use foreach loop for example:
$joins = [
['country', 'user_master.country_id', '=', 'country.country_id'],
['city', 'user_master.city_id', '=', 'city.city_id'],
['login_master', 'user_master.user_id', '=',
'login_master.user_id'],
];
$answers = DB::table('IC2017_AY')
->join('IC2017', 'IC2017_AY.UNITID', '=', 'IC2017.UNITID')
->join('SFA1617_P1', 'IC2017_AY.UNITID', '=', 'SFA1617_P1.UNITID')
->join('cost', 'IC2017_AY.UNITID', '=', 'cost.UNITID')
->join('gpa', 'IC2017_AY.UNITID', '=', 'gpa.UNITID')
->join('enrollment', 'IC2017_AY.UNITID', '=', 'enrollment.UNITID')
->join('major', 'IC2017_AY.UNITID', '=', 'major.UNITID')
->join('preference', 'IC2017_AY.UNITID', '=', 'preference.UNITID');
foreach($joins as $key=>$value){
$answer = $answer->join(implode(",",$value));
}
return $answer->select('IC2017_AY.TUITION2 as in_state_tuition','IC2017_AY.FEE2 as in_state_fee','IC2017_AY.TUITION3 as out_state_tuition','IC2017_AY.FEE3 as out_state_fee','IC2017_AY.CHG4AY3 as books_supplies','IC2017_AY.CHG6AY3 as other_expenses','IC2017.ROOMAMT as room_charge','IC2017.BOARDAMT as board_charge','IC2017.RMBRDAMT as combined_charge','SFA1617_P1.SCUGRAD as total_receiving','SFA1617_P1.SCUGFFN as full_time_receiving','SFA1617_P1.UAGRNTN as total_receiving_grant')
->where($where_data);
->get();
How can I use AS in my count query?
Actually i want to result like { "count":"number" } for json result. I dont know what should i call this thing?
public function firstHourTrades(){
$user_id = Auth::user()->id;
$data = DB::table('finaltrade')
->join('exchanges', 'finaltrade.exchange_id', '=', 'exchanges.id')
->where('finaltrade.user_id', $user_id)
->whereTime(DB::raw('IF(finaltrade.buy_datetime<finaltrade.sell_datetime, finaltrade.buy_datetime, finaltrade.sell_datetime) '), '>=', DB::raw('exchanges.start_time'))
->whereTime(DB::raw('IF(finaltrade.buy_datetime<finaltrade.sell_datetime, finaltrade.buy_datetime, finaltrade.sell_datetime) '), '<=', DB::raw("ADDTIME(exchanges.start_time, '1:00:00')"))
->count();
return response()->json($data);
}
You could also do a raw select and manually assign the alias:
$data = DB::table('finaltrade')
->select(DB::raw('count(*) as count'))
->join('exchanges', 'finaltrade.exchange_id', '=', 'exchanges.id')
->where('finaltrade.user_id', $user_id)
->whereTime(DB::raw('IF(finaltrade.buy_datetime<finaltrade.sell_datetime, finaltrade.buy_datetime, finaltrade.sell_datetime) '), '>=', DB::raw('exchanges.start_time'))
->whereTime(DB::raw('IF(finaltrade.buy_datetime<finaltrade.sell_datetime, finaltrade.buy_datetime, finaltrade.sell_datetime) '), '<=', DB::raw("ADDTIME(exchanges.start_time, '1:00:00')"))
->get();
I am building a query which will be executed every minute when the daily_at time and the current time are equal. The daily_at variable is a timestamp of for example 10:05:33.
I want the where clause in the query to pass when the hour and minute are the same, but not the second else almost non will be executed. The where clause which I have now:
DB::table('restaurants')
->where('schedule', '=', $schedule)
->where('active', '=', 1)
->whereDate('start_at', '<=', Carbon::today())
->where('daily_at', '=', Carbon::now()->toTimeString())
.....
Could someone help me to adjust the where clause to ignore the seconds?
Try with:
DB::table('restaurants')
->where('schedule', '=', $schedule)
->where('active', '=', 1)
->whereDate('start_at', '<=', Carbon::today())
->where('daily_at', '=', Carbon::now()->format('H:i'))
One more way:
DB::table('restaurants')
->where('schedule', '=', $schedule)
->where('active', '=', 1)
->whereDate('start_at', '<=', Carbon::today())
->where('daily_at', '=', Carbon::createFromTime(Carbon::now()->hour,Carbon::now()->minute,00)->format('H:i:s'))
You can pass an extra argument to set timezone:
Carbon::createFromTime(Carbon::now()->hour,Carbon::now()->minute,00,'Example/Zone')->format('H:i:s')
I found the solution. You can use setSeconds(int $value) method:
Carbon::now()->setSeconds(0)->toTimeString()
DB::table('restaurants')
->where('schedule', '=', $schedule)
->where('active', '=', 1)
->whereDate('start_at', '<=', Carbon::today())
->where('daily_at', '=', Carbon::now('Y-m-d H:i')->toTimeString())
use this code
Can you please try with this way:
whereRaw(DATE_FORMAT(daily_at, "%H:%i") = Carbon::now()->format('H:i'))
So final query would be,
DB::table('restaurants')
->where('schedule', '=', $schedule)
->where('active', '=', 1)
->whereDate('start_at', '<=', Carbon::today())
->where('daily_at', '=', Carbon::now()->toTimeString())
->whereRaw(DATE_FORMAT(daily_at, "%H:%i") = Carbon::now()->format('H:i'))
i want to check if the time entered is existed already in the db or not? here in $dbsum and $dbesum i am doing the sum like if i enter starttime 11:30 then it will give 690 in $dbsum and if i enter endtime 12.30 then it will give 750 in $dbesum. 'c_start' and 'c_end' is the sum that already existed in the database.Now i want to check if $dbsum and $dbesum existed between 'c_start' and 'c_end' or not. my query is like below in controller. but it's not working.
$dbstime = explode(':',$request->input('starttime'));
$dbetime = explode(':',$request->input('endtime'));
$dbstime[0] = $dbstime[0]*60;
$dbetime[0] = $dbetime[0]*60;
$dbsum = array_sum($dbstime);
$dbesum = array_sum($dbetime);
$users=DB::table("bookings")
->select("bookdate")
->where([
['bookdate', '=', $request->input('bookdate')],
['roomname', '=', $request->input('roomname')],
['starttime', '=', $request->input('starttime')],
['endtime', '=', $request->input('endtime')],
->whereBetween('c_start', [$dbsum, $dbesum])
->whereBetween('c_end', [$dbsum, $dbesum])
->get();
$users=DB::table("bookings")
->select("id")
->where('bookdate', '=', $request->input('bookdate'))
->where('roomname', '=', $request->input('roomname'))
->where(function ($query){
$query->where($dbsum, '>', 'c_start');
$query->orWhere($dbsum, '<', 'c_end');
})
->orWhere(function($query){
$query->where($dbesum, '>', 'c_start');
$query->orWhere($dbesum, '<', 'c_end');
})
->get();
I have 2 tables 'users' and 'instantUsers'. I want to join them on users.id = instantUsers.user_id and want to add 2 where clauses on the resulting. I'm not getting how to do both. The query I'm using is -
DB::table('users')
->join('instantUsers', function($join) use ($userId) {
$join->on('users.id', '=', 'instantUsers.user_id');
})
->where('instantUsers.instantMode', '=', '1')
->where (function($query) use ($userId) {
$query->where('instantUsers.user_id', '!=', $userId);
})
->get();
You can try this one maybe this will help you:
DB::table('users as table1')->join('instantUsers as table2','table1.id','=','table2.fkId') ->where('table2.instantMode','=','1')->where('table2.user_id','!=',$userId)->get();
Your 'instantUsers.instantMode','=','1' expression can be done in a join, resulting in a better performance.
I would write it like this
DB::table('users')
->join('instantUsers', function($join) use ($userId) {
$join
->on('users.id', '=', 'instantUsers.user_id')
->on('instantUsers.instantMode', '=', 1);
})
->where('users.id', '!=', $userId)
->get();