Laravel / Model / Update data in the DB
Update data in the table
-
STEP
Function Syntax Example Note Find & update() object_Name::update($array_value); $user=User::find($id); $user->update( ['name'=>'manoj', 'email' => 'keval@example.com'] ); 1. object operator ->
2. array parameter in update();
Find & save() Object_Name->save(); $user=User::find($id);
$user->name = $request->name;
$user->save();1. object operator ->
2. no function parameters
3. assign the data to object parametersfirstOrCreate() Model_Name::firstOrCreate($array_conditions, $array_value); $user = User::firstOrCreate(
['name' => 'manoj'],
['email' => 'email@example.com', 'mobile' => '9847']
);1. Static operator ::
2. Two function parameters 3. First parameter is an array used for where condition
4. Second parameteris an array used for updating or creatingfirstOrNew() Model_Name::firstOrNew($array_conditions, $array_value); $user = User::firstOrNew(
['name' => 'manoj'],
['email' => 'email@example.com', 'mobile' => '9847']
); $user->save();1. Static operator ::
2. Two function parameters 3. First parameter is an array used for where condition
4. Second parameteris an array used for updating or creatingupdateOrCreate() Model_Name::updateOrCreate($array_conditions, $array_value); $flight = User::updateOrCreate(
['departure' => 'Oakland', 'destination' => 'San Diego'],
['price' => 99, 'discounted' => 1] );1. Static operator ::
2. Two function parameters 3. First parameter is an array used for where condition
4. Second parameteris an array used for updating or creating1. Find() and update()
read a record using find()
pass the array value to update()
$user=User::find($id); $user->update( ['name'=>'manoj', 'email' => 'keval@example.com'] ); 2. Find() and save()
read a record using find()
set the attribute values
call save() , no function parameters
$user=User::find($id); $user->name = $request->name; $user->save();