99

I'm quite new to laravel and I'm trying to update a record from form's input. However I see that to update the record, first you need to fetch the record from database. Isn't is possible to something like to update a record (primary key is set):

$post = new Post();
$post->id = 3; //already exists in database.
$post->title = "Updated title";
$post->save();
Dester Dezzods
  • 1,417
  • 4
  • 17
  • 33
  • This will be helpful for new users who are getting the same issue: https://www.scratchcode.io/database-records-update-in-laravel/ – Mayank Dudakiya Jan 01 '21 at 09:55

5 Answers5

226
Post::where('id',3)->update(['title'=>'Updated title']);
KaJasB
  • 2,325
  • 2
  • 11
  • 10
  • Does this get Rolled back if something goes wrong? I mean, is this supported by `DB::beginTransaction()` ? – Jonatan Lavado Feb 17 '20 at 20:43
  • of course it works with a `DB::beginTransaction()` since it is a statement that modifies the table, and the database engine will interpret it as a real transaction. – Jeison Flórez Apr 17 '20 at 13:19
  • @Kajasb Is there any to get this updated record without a new query ? – Assad Yaqoob Aug 27 '21 at 06:51
  • 1
    Update on where is a method on [Builder](https://laravel.com/api/10.x/Illuminate/Database/Query/Builder.html#method_update) class from [QueryBuilder](https://laravel.com/api/10.x/Illuminate/Database/Query). Using it would still mean many model features like mutators, global scopes, etc. won't work. [See this](https://stackoverflow.com/q/51096280/10625611). – Qumber Mar 11 '23 at 09:34
83

You can simply use Query Builder rather than Eloquent, this code directly update your data in the database :) This is a sample:

DB::table('post')
            ->where('id', 3)
            ->update(['title' => "Updated Title"]);

You can check the documentation here for more information: http://laravel.com/docs/5.0/queries#updates

Bagaskara Wisnu Gunawan
  • 1,166
  • 1
  • 12
  • 15
33

The common way is to load the row to update:

$post = Post::find($id);

In your case

$post = Post::find(3);
$post->title = "Updated title";
$post->save();

But in one step (just update) you can do this:

$affectedRows = Post::where("id", 3)->update(["title" => "Updated title"]);
Prabhu
  • 5,296
  • 4
  • 37
  • 45
maztch
  • 1,597
  • 19
  • 25
  • @zairwolf: There is nothing wrong in this answer. It is also a good way to fetch record object and update specific field. – Ravi Hirani Feb 26 '16 at 08:58
  • 4
    He wants to avoid fetching the record. I came here because I have a loop in which I want to update some rows, I can't get the records, it'd be too messy and most importante inefficient – Luis Deras May 13 '16 at 19:41
  • read the question first. the user who posted the question has clearly stated that `Without Loading` actually. Your answer is loading the data to update it. – Kiran Maniya Aug 31 '19 at 11:01
25

Use property exists:

$post = new Post();
$post->exists = true;
$post->id = 3; //already exists in database.
$post->title = "Updated title";
$post->save();

Here is the API documentation: http://laravel.com/api/5.0/Illuminate/Database/Eloquent/Model.html

harrrrrrry
  • 13,643
  • 2
  • 23
  • 28
8

You can also use firstOrCreate OR firstOrNew

// Retrieve the Post by the attributes, or create it if it doesn't exist...
$post = Post::firstOrCreate(['id' => 3]);
// OR
// Retrieve the Post by the attributes, or instantiate a new instance...
$post = Post::firstOrNew(['id' => 3]); 

// update record
$post->title = "Updated title";
$post->save();

Hope it will help you :)

Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42