I'm developing in Laravel 5.5 and Php 7.1.9 and I came across the following problem:
I have the following Checkboxes screen and when checking any of the fields, arraydiff
of Laravel
insists on returning the wrong difference value, ie if I check any of the checkboxes of the image it returns 1 => 2
as difference see the image.
ReturnusingLaravel'sArrayDiffunction
CodesnippetgeneratesCheckBox
{{--percorrendoatabelaparaexibiroschecks--}}@foreach($usersas$keys=>$dados_users){{--seorestodadivisãoforparseráexibidonacolunaA--}}@if(!($keys%2))<trclass="">
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td class="">
<p><input type="checkbox" class='chk' name="user_id[]" id="user_id" value="{{ $dados_users->id }}"> {{ $dados_users->name }}</p>
<p><input type="hidden" class='chk' name="user_id_desmarcados[]" id="user_id_desmarcados" checked value="{{ $dados_users->id }} "></p>
</td>
{{-- se não será exibido na coluna B --}}
@else
<td></td>
<td class="">
<p><input type="checkbox" class='chk' name="user_id[]" id="user_id" value="{{ $dados_users->id }}"> {{ $dados_users->name }}</p>
<p><input type="hidden" class='chk' name="user_id_desmarcados[]" id="user_id_desmarcados" checked value="{{ $dados_users->id }} "></p>
</td>
</tr>
@endif
{{-- finalizando o foreach --}}
@endforeach
Controller code with ArrayDiff (Laravel)
public function store(PerfisuserRequests $request)
{
// recebendo todos os dados do formulário
$dataForm = $request->all();
// repassando o valor do Id
$id_perfis = $dataForm['perfis_id'];
/*************************** Verificando os dados que foram Marcados na Inclusão ****************/
/***********************************************************************************************/
// repassando os dados para a variavel collection para verificar com Array Diff
$collection = collect($dataForm['user_id_desmarcados']);
// repassando os dados para a variavel collection para verificar com Array Diff
$collection2 = collect($dataForm['user_id']);
// realizando a verificação atráves do Array Diff
$diff = $collection->diffKeys($collection2);
// aqui será retornado a diferenca entre os Arrays
$diferenca = $diff->all();
dd($diferenca);
For Comparison Effect (after all it was not working the way I wanted it), I decided to use the direct function of php to call array_diff
and to my surprise, the return was favorable, that is, I got the return I needed to see the examples.
Print return with Check 1 checked
ScreenPrintwithCheck2Checked
Array_Diff code snippet
$teste = array_diff($dataForm['user_id_desmarcados'], $dataForm['user_id']);
dd($teste);
So I ask:
Would this be a Bug in the Laravel 5.5 diff array or am I doing something wrong?
Is there any problem in not using the native function of array diff
in laravel?