![]() Reply to this email directly, view it on GitHub, or unsubscribe. This is the upsert method, it adds the timestamps when the model says it allows it. I want the method to be content aware so it updates updated at like the model would if you called save() If none of the columns have changed then it should skip the updated at columnĬurrently it will overwrite the updated at value every time you call upsert for any existing rows that you pass If any of the values I’m passing are different from what’s in the database then I want it to update the updated at column. I want the upsert to do 2 things for rows that already exist. I’m not sure you are understanding what I’m saying. I've managed to achieve it with a QueryBuilder override but it is not a clean solution at this stage and is very targetted to my code base, but thought it might be worthwhile putting here for discussionīeta Was this translation helpful? Give feedback. Would this be something worthwhile for others? If nothing has changed it shouldn't update it. This checks whether any of the fields we are attempting to update has actually changed before changing the updated_at. ON DUPLICATE KEY UPDATE a = values(a), b= values(b), CASE WHEN (a values(a) OR b values(b)) THEN updated_at = values(updated_at) ELSE updated_at = updated_at END In MySQL there is the ability to add logic to the on duplicate key clause The only issue is it ALWAYS updates the updated_at value even when nothing has actually changed. I am wanting to switch to using the upsert method which has an immense improvement in performance. AppServiceProvider.I have a large dataset that gets updated often and am trying to improve performance of the code. ![]() An iterable could be an array, or even a Laravel collection. The name of the macro will be createUpdateOrDelete and it will accept iterable $records. Let's define a macro in the AppServiceProvider boot() method. Let's now combine everything we've learned into one macro. The delete has to happen before the upsert, otherwise the newly added records will be deleted as well. In our case they can all be updated, so we can omit this argument.įor the deleting of the missing ingredients, we can use whereNotIn combined with delete. ℹ️ We could add a third argument to provide an array with the columns that should be updated when a matching record is found. ![]() The second argument lists the column(s) that uniquely identify the records (in this case we use the id). The first argument contains all the ingredient values (note that we have to manually include the foreign key recipe_id). This will require you to loop over all incoming ingredients and pass them into the updateOrCreate method.īefore looking at the macro, let's look at how an upsert works. ⚠️ If you want the updated_at timestamps to be updated correctly, or you rely on Eloquent events, then you should use updateOrCreate instead of upsert. I'll be using upsert since this method can handle multiple records in one go (a single SQL query). ![]() For these two cases we can use Laravel's updateOrCreate or upsert methods. Ingredients with an id should be updated and the ones without an id created. With this information we can determine what ingredients should be created, updated or deleted. name : Cream # Missing in second requestĪs you can see in the hypothetical request data, the second request contains the id's of the existing ingredients. Ingredients could be changed, missing or completely new. $recipe -> ingredients ( ) -> createMany ( [īut that wouldn't work for the subsequent form submissions, these can lead to different scenarios. All the ingredients are new, so we could just create them. The first form submission of our recipe form is straightforward. $recipeįeel free to skip to the macro immediately.īefore dealing with the macro, let's think about how we would handle this problem. In this article I'm going to explain you how to make a macro that allows you to call createUpdateOrDelete() on Eloquent hasMany relationships. A little less obvious is creating, updating and deleting the related models (in this case ingredients). Laravel makes it easy to validate a form like this, thanks to nested array validation. For example, a recipe form with the ability to add ingredients. Create, update or delete hasMany relationships in LaravelĪ pattern that I often encounter when developing forms is a form containing a hasMany relationship.
0 Comments
Leave a Reply. |