I have a database query. It's already working now, but I'm trying to add the unionAll method to this query, I did not do it. I want to combine the two tables with the unionAll method.
$uid = Auth::id();
$homeCovering = DB::table('bid_requests')
->leftJoin('bid_home_coverings', 'bid_requests.id', '=', 'bid_home_coverings.bidId')
->where('bid_requests.userId', '=', $uid)
->where('district', '!=', null)
->get();
$vehicleCovering = DB::table('bid_requests')
->leftJoin('bid_vehicle_coverings', 'bid_requests.id', '=', 'bid_vehicle_coverings.bidId')
->where('bid_requests.userId', $uid)
->where('district', '!=', null)
->get();
Use unionAll() method, Also make sure you really need union all, there is a difference between union and union all
$homeCovering = DB::table('bid_requests')
->select('bid_requests.*')
->leftJoin('bid_home_coverings', 'bid_requests.id', '=', 'bid_home_coverings.bidId')
->where('bid_requests.userId', '=', $uid)
->where('district', '!=', null);
$bid_requests = DB::table('bid_requests')
->select('bid_requests.*')
->leftJoin('bid_vehicle_coverings', 'bid_requests.id', '=', 'bid_vehicle_coverings.bidId')
->where('bid_requests.userId', $uid)
->unionAll($homeCovering)
->where('district', '!=', null)
->get();
Or you could merge these queries as a single query
$bid_requests = DB::table('bid_requests')
->select('bid_requests.*')
->leftJoin('bid_vehicle_coverings', 'bid_requests.id', '=', 'bid_vehicle_coverings.bidId')
->leftJoin('bid_home_coverings', 'bid_requests.id', '=', 'bid_home_coverings.bidId')
->where('bid_requests.userId', $uid)
->where('district', '!=', null)
->get();
If you need distinct bid requests add ->distinct()
Related
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
In my Laravel-5.8 I have these queries:
$manager = DB::table('hr_employees')
->select('line_manager_id')
->where('hr_status',0)
->whereIn('employee_code', $employee_with_goals)
->get();
$employee_with_goals = DB::table('hr_employees')
->join('appraisal_goals', 'hr_employees.employee_code', '=', 'appraisal_goals.employee_code')
->select('hr_employees.employee_code')
->where('hr_employees.company_id', $userCompany)
->where('hr_employees.hr_status',0)
->where('appraisal_goals.is_published', '=', '1')
->where('appraisal_goals.is_approved', '=', '3')
->groupBy('hr_employees.employee_code')
->get();
$manager is the main query
How do I combine these two queries as one using $employee_with_goals as the variable in whereIn()?
You can use the pluck method to retrieve all values for a given key in a collection:
$manager = DB::table('hr_employees')
->select('line_manager_id')
->where('hr_status',0)
->whereIn('employee_code', $employee_with_goals->pluck('employee_code'))
->get();
You can actually use a subquery too:
$manager = DB::table('hr_employees')
->select('line_manager_id')
->where('hr_status',0)
->whereIn('employee_code', function ($query) use ($userCompany) {
$query
->table('hr_employees')
->select('hr_employees.employee_code')
->join('appraisal_goals', 'hr_employees.employee_code', '=', 'appraisal_goals.employee_code')
->where('hr_employees.company_id', $userCompany)
->where('hr_employees.hr_status',0)
->where('appraisal_goals.is_published', '=', '1')
->where('appraisal_goals.is_approved', '=', '3')
->groupBy('hr_employees.employee_code')
})
->get();
The code above is untested.
Larvel have a question about inner joins with multiple tables and on values. I did build my code like this in laravel.
$clients = clients::leftjoin('clients_payment_type','clients_topup', function($join) {
$join->on('clients_payment_type.user_id', '=', 'clients.id') AND
$join->on('clients_topup.user_id', '=', 'clients.id');
})->where('clients.reference_id','=',$reference_id)->get();
Try this :
$clients = clients::leftjoin('clients_payment_type','clients_topup')
->join('clients','clients_payment_type.user_id', '=', 'clients.id')
->join('client_toup','clients.id', '=', 'clients_topup.user_id')
->where('clients.reference_id','=',$reference_id)
->get();
Regards
Working Answer
$clients1 = DB::table('clients')
->leftjoin('clients_payment_type','clients_payment_type.user_id', '=', 'clients.id')
->leftjoin('clients_topup', 'clients_topup.user_id', '=', 'clients.id')
->where('clients.reference_id','=',$reference_id)
->get();
I have this sql syntax
SELECT vehicles.license_plate, vehicles.year, vehicles.status, vehicles.zone_id, vehicles.car_class_id, harga_sewa.value, harga_sewa.description
FROM `vehicles`
RIGHT JOIN `harga_sewa` ON vehicles.zone_id = harga_sewa.zone_id
AND vehicles.car_class_id = harga_sewa.car_class_id
How I can write something like this?
DB::table('vehicles')
->rightJoin('harga_sewa', 'vehicles.zone_id', '=', 'harga_sewa.zone_id', AND vehicles.car_class_id = harga_sewa.car_class_id)
->select(*)
Try below code
DB::table('vehicles')
->rightJoin('harga_sewa', function ($join) {
$join->on('vehicles.zone_id', '=', 'harga_sewa.zone_id');
$join->on('vehicles.car_class_id', '=', 'harga_sewa.car_class_id');
})
->get();
On mobile, so can't test, but from memory this should work:
DB::table('vehicles')
->rightJoin('harga_sewa', 'vehicles.zone_id', '=', 'harga_sewa.zone_id')
->rightJoin('harga_sewa', 'vehicles.car_class_id', '=', 'harga_sewa.car_class_id')
->select('*')
->get();
I have this code but I am not sure if is possible be more compact or I´m not sure if this is the way should looks this type of queries in laravel.
public function getRoleTransactionNameByID($id)
{
$role_transaction= $this->model->select(
'roles_transactions.id',
'roles.id as role_id',
'roles.role_name',
'modules.module_name',
'transactions.transaction_name',
'transactions.transaction_description',
'roles_transactions.transaction_action_id',
'transaction_actions.transaction_action_name',
'roles_transactions.created_at',
'roles_transactions.updated_at')
->join('roles', 'roles_transactions.role_id', '=', 'roles.id')
->join('transactions', 'roles_transactions.transaction_id', '=', 'transactions.id')
->join('transaction_actions', 'roles_transactions.transaction_action_id', '=', 'transaction_actions.id' )
->leftjoin('modules','transactions.module_id', '=', 'modules.id')
->where('roles_transactions.id', '=', $id)
->where('roles_transactions.deleted_at','=', null)
->orderby('roles.role_name', 'ASC')
->orderby('modules.module_name', 'ASC')
->orderby('transactions.transaction_name', 'ASC')
->get();
return $role_transaction;
}
Thank you for the help.