Laravel 5.5 Array Diff with bug?

Asked

Viewed 89 times

1

I am developing in Laravel 5.5 and Php 7.1.9 and came across the following problem:

I have the following Checkboxes screen and when marking any of the fields, the arraydiff of Laravel insists on returning the value of the incorrect difference, ie if I check any of the image checkbox it returns 1 => 2 As difference see the image.

Screen Print inserir a descrição da imagem aqui

Return using the Laravel Arraydif function inserir a descrição da imagem aqui

Code snippet generates Checkbox

{{-- percorrendo a tabela para exibir os checks--}}
@foreach($users as $keys => $dados_users)                                                
        {{-- se o resto da divisão for par será exibido na coluna A --}}                           
        @if(!($keys % 2))
            <tr class="">    
                <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 wasn’t working the way I wanted), I decided to use the direct php function to call the array_diff and to my surprise the return was favorable, IE, I got the return I needed see the examples.

Return print with Check 1 marked inserir a descrição da imagem aqui

Screen Print with Check 2 Marked inserir a descrição da imagem aqui

Excerpt from Array_diff Code

 $teste = array_diff($dataForm['user_id_desmarcados'], $dataForm['user_id']);

    dd($teste);

So I ask:

That would be a bug in the diff array of Laravel 5.5 or am I doing something wrong?

Is there a problem not using the native function of array diff in the Laravel?

  • You wanted the difference and extract the values, you’re confused. the function to configure is diff in the case!

  • yes! needed the difference! what I wanted to show is that the Laravel diff array_is not working properly! the diff was being used

  • You didn’t use it right and the array_diffis not Laravel is PHP! use $collection->diff the one you use... even does the same thing using diff arra_f!

  • I think q Voce did not understand the code... yes I know q the array_diff is php .. but Laravel has its own method.. the first time I used Collection...

  • I did, and Laravel has a class Collection to work with this, I know all this, but, there is no bug, what may be happening is a mess of yours, something I would like you to say could rephrase the question giving example of how you want the result and how it is not working, this is very confusing if you can, the example is minimal giving to understand, the way you are not using $collection->diff if you’re using $collection->diffKeys and that’s what I’m seeing in your question

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.