Bug change position of laravel site banners

1

I am developing a site where it has a modal banners and want to set a field in the database of the "order", where I can decide which banner appears first, second, third ... in my admin panel I I have a list of all banners and banner that is third I need to set it to first, fourth to second, first to third, that is, you can change their order after registered. I used the code below, it works only 3 times and then it starts to bug, it does not work. I really need help to do this, set order for them and save the order in the database.

private function ordenaPosicionamento($posicaoAntiga, $novaPosicao)
    {
        if($novaPosicao == 1)
        {
            // busca todos banners num array
    
            $banners = Banner::where('status', '=', true)->get();

            // busca o banner para ser alterado e seta um valor temporario para alteracao
    
            $bannerParaAlteracao = $banners[$posicaoAntiga - 1];
            $bannerParaAlteracao->ordem = 0;

            // sobe a posicao dos anteriores
    
            for($i2 = $posicaoAntiga; $i2 < count($banners); $i2++)
            {
                $banners[$i2]->ordem = $banners[$i2]->ordem - 1;
            }

            // ajusta a posicao de todos {final}
    
            for($i4 = 0; $i4 < count($banners); $i4++)
            {
                $banners[$i4]->ordem = $banners[$i4]->ordem + 1;
                //echo "NOME " . $banners[$i4]->nome . " ORDEM " . $banners[$i4]->ordem . "<br>";
                //$banners[$i4]->save();
            }

            foreach($banners as $banner)
            {
                $banner->save();
            }
        }
    }

My database has the following fields: id, status, order, name

    
asked by anonymous 22.09.2016 / 17:03

2 answers

1

You can try to use Collections, maybe it will get easier. See if this helps:

$banners = Banner::where('status', '=', true)->get()->sortBy('ordem');
$b = $banners->get($posicaoAntiga-1);
$array = $banners->forget($posicaoAntiga-1)->sortBy('ordem')->all();
array_splice( $array, $posicaoNova-1, 0, [$b] );
foreach ($array as $key => $a) {
    $a->ordem = $key+1;
    $a->save();
}

In this code, the return collection is sorted by ordem . After this, the element is removed from the old position and the collection is rearranged to generate new keys. Then we insert the element into the new position by converting the collection to array with all() and using array_splice. Then just save the new order and save.

See if it works for you.

    
22.09.2016 / 17:30
0

This code only works if you change a banner to the first position

// pega o banner que queremos alterar e seta para zero
        DB::table('banners')->where('ordem', '=', $posicaoAntiga)->update(['ordem' => 0]);

        // desce a posicao dos anteriores
        for($i2 = $posicaoAntiga; $i2 <= $this->quantidadeAtivos(); $i2++)
        {
            $nova = $i2 - 1;
            DB::table('banners')->where('ordem', '=', $i2)->update(['ordem' => $nova]);
        }

        // ajusta a posicao de todos {final}

        for($i4 = $this->quantidadeAtivos(); $i4 >= 0; $i4--)
        {
            $nova2 = $i4 + 1;
            DB::table('banners')->where('ordem', '=', $i4)->update(['ordem' => $nova2]);
        }
    
22.09.2016 / 19:28