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
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 ...
– Danilo Tiago Thai Santos
I edited the code. I tested again here and was counting one more position. Try with this code I subtracted 1 from the positions.
– Laerte
Cara did not work here, but I did otherwise, a gambiarra that solves my problem. Even so thank you for the attention :D
– Danilo Tiago Thai Santos
All right!! I’m glad it worked out!!
– Laerte
Put it there for us to see, bro...
– Diego Souza