In the previous example, the query is the same, only the parameters is different, so I can use a function for it.
How can I achieve the same, if the base of the query is the same, but different get() will need different set of where conditions?
How do I reuse the 'base of the query' in such a case?
Function won't work due to the differences in orWhere clauses
$securedContractSum = DB::table('project_registry')
->select(DB::raw("count(Project_Code) as totalJobs, round(sum(contract_original_value)/1000,2) as monthlySecuredSum, date_format(Project_Date_Prepared, '%Y-%m') as YearMonth"))
->groupBy('YearMonth')
->whereBetween('Project_Date_Prepared', [$from, $to])
->where(function($query) {
$query->where('project_code', 'like', '%1')
->orWhere('project_code', 'like', '%2')
->orWhere('project_code', 'like', '%3')
->orWhere('project_code', 'like', '%4')
->orWhere('project_code', 'like', '%5')
->orWhere('project_code', 'like', '%6')
->orWhere('project_code', 'like', '%7')
->orWhere('project_code', 'like', '%8')
->orWhere('project_code', 'like', '%9')
->orWhere('project_code', 'like', '%0');
})
->where(function($query) {
$query->where('project_status', '<>', 'cancelled');
});
$securedContractSumX = $securedContractSum
->where(function($query) {
$query->where('project_code', 'like', 't%');
})
->where(function($query) {
$query->where('project_type', 'like', '%X%')
->orWhere('project_type', 'like', 'Y%')
->orWhere('project_type', 'like', '%Z%')
->orWhere('project_type', 'like', '%A%');
})
->where(function($query) {
$query->where('project_client', 'like', '%B%');
})
->get();
$securedContractSumTNBGrid = $securedContractSum
->where(function($query) {
$query->where('project_code', 'like', 't%');
})
->where(function($query) {
$query->where('project_type', 'like', '%ZX%')
->orWhere('project_type', 'like', '%XXX%')
->orWhere('project_type', 'like', '%YYY%')
->orWhere('project_type', 'like', '%DDD%')
->orWhere('project_type', 'like', '%BBB%')
->orWhere('project_type', 'like', '%AAA%');
})
->where(function($query) {
$query->where('project_client', 'like', '%SSS%');
})
->get();