Home > database >  How to use Where() with save() method?
How to use Where() with save() method?

Time:07-17

I'm looking for away to use Where condition instead of find(), but with save() method

this is my code :

$flight = App\Flight::find(1);   // I want to use ::where([multiple conditions])

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

$flight->save();

I tried but i'm getting an error

CodePudding user response:

You can use ->first()

$flight = Flight::where('active', 1)
      ->where('destination', 'San Diego')
      ->first();

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

$flight->save();

More information: https://laravel.com/docs/9.x/eloquent#retrieving-single-models

  • Related