Will Model::updateOrCreate() update a soft-deleted model if the criteria matches?

laravel updateorcreate
laravel update query
eloquent update record
user soft delete
soft delete php
laravel restore soft delete
laravel soft delete deleted_by
laravel 6 soft delete

Let's say I have a model that was soft-deleted and have the following scenario:

// EXISTING soft-deleted Model's properties
$model = [
    'id'         => 50,
    'app_id'     => 132435,
    'name'       => 'Joe Original',
    'deleted_at' => '2015-01-01 00:00:00'
];

// Some new properties
$properties = [
    'app_id'     => 132435,
    'name'       => 'Joe Updated',
];

Model::updateOrCreate(
    ['app_id' => $properties['app_id']],
    $properties
);

Is Joe Original now Joe Updated?

OR is there a deleted record and a new Joe Updated record?

updateOrCreate will look for model with deleted_at equal to NULL so it won't find a soft-deleted model. However, because it won't find it will try to create a new one resulting in duplicates, which is probably not what you need.

BTW, you have an error in your code. Model::updateOrCreate takes array as first argument.

laravel, updateOrCreate will look for model with deleted_at equal to NULL so it won't find a soft-deleted model. However, because it won't find it will try to create a new  Mathieu Dierckx. Apparently, this user prefers to keep an air of mystery about them. 5. 1 Will Model::updateOrCreate() update a soft-deleted model if the criteria

$variable = YourModel::withTrashed()->updateOrCreate(
    ['whereAttributes' => $attributes1, 'anotherWhereAttributes' => $attributes2],
    [
        'createAttributes' => $attributes1,
        'createAttributes' => $attributes2,
        'createAttributes' => $attributes3,
        'deleted_at' => null,
    ]
);

create a new OR update an exsiting that was soft deleted AND reset the softDelete to NULL

Eloquent: Getting Started - Laravel, Soft Deleting; Querying Soft Deleted Models. Replicating Models; Query The Eloquent all method will return all of the results in the model's table. Since each  The same is true for the key vault. In order to permanently delete a soft-deleted key vault and its contents, you must purge the key vault itself. Purging a key vault. When a key vault is purged, its entire contents are permanently deleted, including keys, secrets, and certificates. To purge a soft-deleted key vault, use the az keyvault purge

Model::withTrashed()->updateOrCreate([
   'foo' => $foo,
   'bar' => $bar
], [
   'baz' => $baz,       
   'deleted_at' => NULL     
]);

Works as expected (Laravel 5.7) - updates an existing record and "undeletes" it.

Model::firstOrNew() with soft deleted., Model::firstOrNew() with soft deleted. Posted 5 years ago by jostrander. I am looking to find all objects whether trashed or not using the firstOrNew if possible. And, when querying a model that uses soft deletes, the soft deleted models will automatically be excluded from all query results. To determine if a given model instance has been soft deleted, use the trashed method:

RoleUser::onlyTrashed()->updateOrCreate(
            [
                'role_id' => $roleId,
                'user_id' => $user->id
            ],
            [
                'deleted_at' =>  NULL,
                'updated_at' => new \DateTime()
            ])->restore();

Like this you create a new OR update an exsiting that was soft deleted AND reset the softDelete to NULL

Using updateOrCreate for multiple data insert in Laravel, I have learnt the usage of Model::updateOrCreate and used it in some of my modules Check this link. http://laravel.com/docs/4.2/eloquent#insert-update-​delete Second Way Mannually If no matching model exists, create one. $​myDataArray, ''); To know more about loadMultiple() you can check symbolic-​computation 1 INTRODUCTION. There is increasing evidence that respiratory syncytial virus (RSV) is a common cause of respiratory tract infections (RTI) in adult patients, 1 often with a complicated course of disease. 2-5 Among hospitalized elderly ≥65 years of age mortality is as high as 8%, 2 but among high‐risk groups as patients with chronic heart or lung disease, long‐term care facility

I tested the solution by @mathieu-dierckxwith Laravel 5.3 and MySql If the model to update has no changes (i.e. you are trying to update with the same old values) the updateOrCreate method returns null and the restore() gives a Illegal offset type in isset or empty

Eloquent, Soft Deleting; Querying Soft Deleted Models. Query Scopes The Eloquent all method will return all of the results in the model's table. Since each Eloquent  Windows Update will automatically install it on your PC. If you’re using Windows 10, you likely already have it installed. We’ve installed this update on several PCs and haven’t run into the bug. If your PC has already installed the update and you haven’t experienced the bug, you don’t need to uninstall the update or take any action.

laravel model Code Example, If no matching model exists, create one. 3. $flight = App\Flight::updateOrCreate(. 4. ['departure' => 'Oakland', 'destination' => 'San Diego'],. 5. Laravel Repositories. A package that provides a neat implementation for integrating the Repository pattern with Laravel & Eloquent. Disclaimer. This package was originally released here, but since the future of that package is not clear, it has been forked and re-worked under this repository.

Model, Helper method to determine if a instance is "soft deleted". public async update(​values: object, options: object): Promise<Model>. This is the If you provide an include option, the number of matching associations will be counted instead. findAll() // WHERE email like 'dan@sequelize.com%' AND access_level >= 42  An update will be executed if a row which matches the supplied values on either the primary key or a unique key is found. Note that the unique index must be defined in your sequelize model and not just in the table.

Laravel5.7 eloquent ORM quick start walkthrough, Each data table corresponds to a model that interacts with the table. Through the model class, you can query, insert, update, delete and other  Soft-deleted resources are retained for a set period of time, 90 days. During the soft-delete retention interval, the following apply: You may list all of the key vaults and key vault objects in the soft-delete state for your subscription as well as access deletion and recovery information about them.

Comments
  • Thanks. I updated the code. Also, I tried Model::withTrashed()->updateOrCreate() but got an exception since it tried to use the query builder instead of the eloquent model.
  • It's normal that you're getting an exception cause that's exactly what you're doing - calling updateOrCreate() on a Builder that is returned from withTrashed(). You're trying to misuse the soft deleting trait. It's not designed as a tool to hide records. Regardless whether a model is deleted or soft-deleted you should treat it the same way - there is no such record, so you do not update it. The only operation that you should be able to do is to restore a soft-deleted model. If you really want to update it you can use DB facade.
  • I'm actually getting contacts from another external database and saving them, and then pushing them up to hubspot. The database I'm getting them from has bad emails and duplicates. I'm soft deleting all records with bad emails, but when I re-sync I want to update records that have may have fixed their emails and restore them, instead of adding a new record, since they aren't actually new. I have since just added a new method updateOrCreateWithTrashed... maybe I should call it updateOrCreateAndRestoreIfFixed :P
  • Just a note here - in 5.5 at least, Model::withTrashed()->updateOrCreate() is valid and works as expected, with the exception that the record is not restored. I couldn't chain the restore() method either, so had to check if the returned model was trashed() then call restore().
  • Model::withTrashed()->updateOrCreate() works in L5.4 [tested]
  • Nice solution, just remember to add 'deleted_at' to the fillable property of the model! ;)
  • Thank you, @Nem, I'd forgotten that step.
  • Model remains deleted in Laravel 5.8. It's possible to undelete by Model::withTrashed()->updateOrCreate(['foo' => $foo], ['baz' => $baz])->restore()