Here's my issue : I have a table in sqlserver database as follow :
Product (
id int identity,
reference varchar(12),
name varchar(40)
)
In a web page of a laravel web-app, I got a form with name only as field and a submit button. In DB side, I created a trigger (Instead of insert) that updates the reference of the row based on the generated id. Example :
RGX0000123
Where 123 is the generated ID and RGX is randomly generated.
The flow follows Post/Redirect/Get design pattern. After the submit, I want to redirect to a page where the reference is shown in the URL and not the ID.
In laravel's controller, I save my object with Eloquent and redirect to the next page :
$product->save();
return redirect()->route('next_page', ['reference' => $product->reference]);
My problem is that I can get the id after the save but not the reference. I don't know how Eloquent works but with Hibernate(JAVA) such thing can be resolved with a session.flush() to sync the object with data from the database.
Quick and dirty fix was using
$product= Product::find($product->id);
Is there a cleaner way to handle this ?
save() save() method is used both for saving new model, and updating existing one. here you are creating new model or find existing one, setting its properties one by one and finally saves in database.
We can update the records using the DB facade with update method. The syntax of update method is as shown in the following table. Run an update statement against the database.
In eloquent ORM, $fillable attribute is an array containing all those fields of table which can be filled using mass-assignment. Mass assignment refers to sending an array to the model to directly create a new record in Database.
You can update the record which you recently saved in your database using save method in Laravel. You can get the id of recently saved record and after that you can use update method with where condition to update the record. Using this code snippet you can update the recently added record.
Here is a bit of a Laravel eloquent Model.php : // If the model already exists in the database we can just update our record // that is already in this database using the current IDs in this "where" // clause to only update this model. Otherwise, we'll just insert them. if ($this->exists) { $saved = $this->isDirty() ?
To see if an attribute has been changed see Laravel check if updateOrCreate performed update Here is a bit of a Laravel eloquent Model.php : // If the model already exists in the database we can just update our record // that is already in this database using the current IDs in this "where" // clause to only update this model.
The best way to achieve before and after save callbacks in to extend the save () function. So now when you save a Page object its save () function get called which includes the parent::save () function; Show activity on this post. Show activity on this post. Actually, Laravel has real callback before|after save|update|create some model. check this:
Try refreshing the object:
$product->save();
$product->refresh(); // <---
return redirect()->route('next_page', ['reference' => $product->reference]);
From the documentation:
Refreshing Models
You can refresh models using the
fresh
andrefresh
methods. Thefresh
method will re-retrieve the model from the database. The existing model instance will not be affected:$flight = App\Flight::where('number', 'FR 900')->first(); $freshFlight = $flight->fresh();
The
refresh
method will re-hydrate the existing model using fresh data from the database. In addition, all of its loaded relationships will be refreshed as well:$flight = App\Flight::where('number', 'FR 900')->first(); $flight->number = 'FR 456'; $flight->refresh(); $flight->number; // "FR 900"
You can use refresh() method for refresh data in model
$model->refresh();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With