Create function to change a data in the database table

Asked

Viewed 32 times

0

Good morning, I’m doing a function so I can alter a data in a database table. That is, I have 4 data recorded in my database table where the main is 'yes', when recording a fifth data, the second oldest data has to go to 'no' in main.

public function removeMain(){
        $news = News::Where('main' == 'Sim')->get();
        if(count($news) == 4){
            $news = News::Where('id','=', $news[2]->id)->first();
            $news->main = 'Não';
        }
    }

The code I already have is this but it still doesn’t change the data in main. Can someone give me a help?

1 answer

1


Documentação Laravel:

$flight = App\Flight::find(1);

$flight->name = 'New Flight Name';

$flight->save();

That is, you search for the data, change what you need and save it after the change.

In your case, I think you’ll solve it just by adding $news->save() after $news->main = 'Não';.

I hope it helps!

Ref.: Update Eloquent - Laravel

  • 1

    Thanks for the help!

Browser other questions tagged

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