ChatGPT解决这个技术问题 Extra ChatGPT

Laravel eloquent update record without loading from database

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();
This will be helpful for new users who are getting the same issue: scratchcode.io/database-records-update-in-laravel

K
KaJasB
Post::where('id',3)->update(['title'=>'Updated title']);

Confirmed, this is the Eloquent way to update without performing a fetch
I wish this answer would be shown first as it's the correct answer - use Eloquent, not Query Builder.
Does this get Rolled back if something goes wrong? I mean, is this supported by DB::beginTransaction() ?
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.
@Kajasb Is there any to get this updated record without a new query ?
B
Bagaskara Wisnu Gunawan

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


Thanks a million .. You made my day
no because then you lose your timestamps and other model behaviors
@malhal unfortunately, I haven't found any solution to this case fixed using Eloquent, because Eloquent basically creates objects for each rows to be able to have all those features. so in order to update them without loading and updating each rows is simply impossible. CMIIW
How do you check, in this case, if the given row was indeed updated? Like a if ($data->save())?
@malhal see below answer by KaJasB for anyone who want to use eloquent for the update.
h
harrrrrrry

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


honestly, this is kinda difficult to read.
P
Prabhu

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"]);

@zairwolf: There is nothing wrong in this answer. It is also a good way to fetch record object and update specific field.
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
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.
R
Ravi Hirani

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 :)