Laravel.如何从mergedEmployees中获取removedEmployees?

piok6c0g  于 2023-11-20  发布在  其他
关注(0)|答案(1)|浏览(129)

$removedEmployees = $mergedEmployees->diff($uniqueEmployees);

这是我的代码。

$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);

字符串

bxgwgixi

bxgwgixi1#

Laravel合并状态
第一个月
这里看到
所以这行$mergedEmployees = $employees1->merge($employees2);已经创建了一个唯一的数组
因此,$mergedEmployees->diff($uniqueEmployees)结果为空
你应该做的是...

$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))))));

字符串
这里有一个缺点,它不会处理多个副本,如果$mergedEmployees像array(1,1,1,2),$uniqueEmployees像array(1,2),结果将是array(1)

相关问题