Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP / Laravel : Retrieve new info of Model after save()

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 ?

like image 721
Wildfire Avatar asked Oct 21 '19 15:10

Wildfire


People also ask

What is Save () in Laravel?

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.

How do you update a record in Laravel?

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.

What is fillable in Laravel model?

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.

How to update a saved record in Laravel 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.

How do I update an existing model in Laravel?

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() ?

How to see if an attribute has been changed in Laravel?

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.

How to get before and after save callbacks in Laravel?

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:


2 Answers

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 and refresh methods. The fresh 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"
like image 71
Kenny Horna Avatar answered Sep 21 '22 12:09

Kenny Horna


You can use refresh() method for refresh data in model

$model->refresh();

like image 41
Levon Babayan Avatar answered Sep 22 '22 12:09

Levon Babayan