skip to Main Content

this code is not working: $removedEmployees = $mergedEmployees->diff($uniqueEmployees);

here is my code.

$employees1 = Employee::where('employment_status', '<>', 'SEPARATED')
    ->where('client_code_1', $client->client_code)
    ->where('position', $grouping->position)
    ->where('billable_rate', $grouping->billable_rate)
    ->get();

$employees2 = Employee::where('employment_status', '<>', 'SEPARATED')
    ->where('client_code_2', $client->client_code)
    ->where('position', $grouping->position)
    ->where('billable_rate', $grouping->billable_rate)
    ->get();

$mergedEmployees = $employees1->merge($employees2);
$uniqueEmployees = $mergedEmployees->unique();
$removedEmployees = $mergedEmployees->diff($uniqueEmployees);

2

Answers


  1. laravel merge states

    The merge method merges the given array or collection with the original collection. If a string key in the given items matches a string key in the original collection, the given item's value will overwrite the value in the original collection:

    see here

    so this line
    $mergedEmployees = $employees1->merge($employees2);
    is already creating a unique array

    as a result, $mergedEmployees->diff($uniqueEmployees) results empty

    Instead you should do…

    
    $employees1 = Employee::where('employment_status', '<>', 'SEPARATED')
        ->where('client_code_1', $client->client_code)
        ->where('position', $grouping->position)
        ->where('billable_rate', $grouping->billable_rate)
        ->get()->toArray();
    
    $employees2 = Employee::where('employment_status', '<>', 'SEPARATED')
        ->where('client_code_2', $client->client_code)
        ->where('position', $grouping->position)
        ->where('billable_rate', $grouping->billable_rate)
        ->get()->toArray();
    
    $mergedEmployees = array_merge($employees1, $employees2);
    $uniqueEmployees = array_unique($mergedEmployees,SORT_REGULAR);
    
    
    $removedEmployees = collect(array_map('json_decode',array_keys(array_diff_assoc(array_flip(array_map('json_encode', $mergedEmployees)),array_flip(array_map('json_encode', $uniqueEmployees))))));
    

    there is a shortcoming here though,it will not handle more then one duplicate, if $mergedEmployees is like array( 1,1,1,2) and $uniqueEmployees is like array( 1,2), the result will be array( 1 )

    Login or Signup to reply.
  2. The code you provided has an issue because $uniqueEmployees is already a unique collection, so when you try to get the difference between $mergedEmployees and $uniqueEmployees, there won’t be any elements left in $removedEmployees.

    The diff method in Laravel’s collections returns the items in the collection that are not present in the given collection. Since $uniqueEmployees contains distinct values from $mergedEmployees, there will be no difference, and $removedEmployees will be an empty collection.

    If you want to find the items that are in one collection but not in the other, you should use diff between the original collections $employees1 and $employees2 before merging them:

    $employees1 = Employee::where('employment_status', '<>', 'SEPARATED')
        ->where('client_code_1', $client->client_code)
        ->where('position', $grouping->position)
        ->where('billable_rate', $grouping->billable_rate)
        ->get();
    
    $employees2 = Employee::where('employment_status', '<>', 'SEPARATED')
        ->where('client_code_2', $client->client_code)
        ->where('position', $grouping->position)
        ->where('billable_rate', $grouping->billable_rate)
        ->get();
    
    $removedEmployees = $employees1->diff($employees2);
    $mergedEmployees = $employees1->merge($employees2);
    $uniqueEmployees = $mergedEmployees->unique();
    

    This code will give you the employees that are in $employees1 but not in $employees2 in the $removedEmployees collection.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search