Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Soft deleting in pivot with sync method laravel

I want sync some relation in User's Table and I don't want to Laravel delete the row, I want make some updates on that row (Like fill the deleted_at), I searched so far and only solution i found is that to override sync method.

So how i can override the sync method to update the row?

Or whats another solution for this problem?

EDITED: I know the problem is with detach method, If i could override the detach It will be solved!

Thanks

like image 517
Katerou22 Avatar asked Dec 16 '17 07:12

Katerou22


People also ask

How soft delete is implemented in Laravel?

Soft delete hides information from end-user or flags data as deleted while it still remains visible or active in your database. To perform soft delete in Laravel, you need to have a deleted_at column that should be set to default null , as it should be of timestamp data type in the model.

How do I delete a pivot table in Laravel?

To delete all records on the pivot table for a model, you may use the detach method: User::find(1)->roles()->detach();

What is the advantage of soft delete in Laravel?

The advantage of soft-delete concept is, as you never physically delete the data, there is no risk of loss of data when something goes wrong (with the delete action, not with your code). It's easy to get back the record by just changing the flag.

What is force delete in Laravel?

Force Delete (Permanently) If we want to delete permanently, we have this option: Product::find($id)->forceDelete(); To delete from soft-deleted (trashed) data, we need to write code like: Product::onlyTrashed()->find(2)->forceDelete(); We can also set conditions at the time of deleting data from the trash.


1 Answers

I think you were looking for this method.

$user->your_relation()->updateExistingPivot($userid, ['deleted_at' => $date]);
like image 188
Erubiel Avatar answered Sep 30 '22 08:09

Erubiel