Bug change position of banners site Laravel

Asked

Viewed 73 times

1

I’m developing a website where it has a banner modal and I want to define a field in the database of type "order", where I can define which banner appears in first, second, third ... on my admin panel I have a listing of all banners and the banner that is in third I need to set it to first, fourth to second, first to third, ie, be able to change their order after registered. I used the code below, it works only 3 times and then starts to bug, it doesn’t 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

2 answers

1

You can try using Collections, maybe it gets 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 ordered by ordem. After this, the element is removed from the old position and the collection is reordered to generate new keys. Then we insert the element in 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 your case.

  • So it still hasn’t worked, for example, I have 5 banners, I need to pick the third and set it to first, the code you passed the banner when I change the select to 1, it returns a position ...

  • I edited the code. I tested again here and was counting one more position. Try with this code I subtracted 1 from the positions.

  • Cara did not work here, but I did otherwise, a gambiarra that solves my problem. Even so thank you for the attention :D

  • All right!! I’m glad it worked out!!

  • Put it there for us to see, bro...

0

This code works only to 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]);
        }
  • Anyway it would need to work for any position, the first turns third, the second turns first, the last turns first and the fourth turns last for example.

Browser other questions tagged

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