I have a complaints
table, which stores all complaints a user register, and these complaints are assigned to some users to solve them.
I don't have any relational table which maps which complaint is assigned to which user, indeed i have a table employees_records
which stores information of all users to whom some complaints are assigned.
(since these codes are on a live server, we cant change the db structure for now)
For now we get which complaint is assigned to which user is via
$v=Complaints::find($id);
$dUser = EmployeesRecord::whereJsonContains('categories', "$v->category_id")
->whereJsonContains('sub_categories', "$v->code_id")
->whereJsonContains('zones', "$v->zone_id")
->whereJsonContains('wards', "$v->ward_id")
->whereJsonContains('cities', "$v->city_id");
->where('department_id',$v->department_id)->first();
if $dUser
is empty, it means complaint is not assigned to any user and if we get a user, it means complaint is assigned to that user.
At the time of complaint registration, we get categories,sub_categories, zones, wards, cities,department_id data. Which is checked against that any user is under that IDs.
FINALLY THE PROBLEM :
I want to get all complaints which are not assigned to any user. But the process to know whether the complaint is assigned or not to a user (as I have mentioned above) is complicated to me. And I am not able to map this thing in a query. I want a query with high performance since I am using this in a report generation.
What i tried.
I tried to get all the complaints first
$complaints=Complaints::all();
$assigned_complaints=[];
$un_assigned_complaints=[];
foreach($complaints as $k => $v)
{
$dUser = EmployeesRecord::whereJsonContains('categories', "$v->category_id")
->whereJsonContains('sub_categories', "$v->code_id")
->whereJsonContains('zones', "$v->zone_id")
->whereJsonContains('wards', "$v->ward_id")
->whereJsonContains('cities', "$v->city_id");
->where('department_id',$v->department_id)->first();
if($dUser)
{
$assigned_complaints[]=$k;
}
else
{
$un_assigned_complaints[]=$k;
}
}
$total_assigned_complaints=count($assigned_complaints);
$total_un_assigned_complaints=count($un_assigned_complaints);
But i want a solution, which applies on the Complaint
model query not with looping the Complaints data.
As the data is very large in number.