my current project i work on require softDelete functionality, And boom you shot a video on same topic. man did you read mind?! Thanks alot. You save my day.
In line with the unique rule, I suggest "renaming" the unique column before soft deleting it, like adding a suffix "-trashed". Then it's up to you if you will change it to "-restored" after restoring it (i think it's possible). eg: Title: "abc" -> "abc-trashed" (softdeleted) -> "abc-restored" (after restore)
Thanks Povilas, very informative as usual We can enable route model binding for soft deleted models as well, the syntax is: Route::get('/posts/{post}', [PostController::class, 'show'])->withTrashed();
Great wrap up. Over-riding the Laravel default regarding validation would make the restoration step a lot more complex - I wonder how you would tackle checking whether a restoration is permissible (that it won't restore a model that breaks the unique constraint for example).
This package is useful when we have multiple relations to delete. For me I think $user->posts()->delete() will be fine. I have a question should we use soft delete on pivot table if parents table uses soft delete?
Wow, what a useful video--thank you! I don't now how the 'archive' got on the request @ 1m50s. I'll have to research that. I appreciate your making this.
3:05 Thanks a zillion for this! I tried to normally use forceDelete() and restore() method for those action according to the official docs. But it seemed it doesn't work. I tried a couple time but still had no idea why the heck the model aren't getting restored or deleted. Phew, well, so I have to find the trashed/softdeleted post at first like this: `` Post::onlyTrashed()->findOrFail($id); `` then attempt to delete it. Great!
Thank you for sharing your experience & tips with us, also for a clear pronunciation for non-english-native-speakers (kind of hard to understand more of the time to me). btw, do you recommend this method with the following scenario >> let's say besides setting value to "deleted_at" field, I set one or more fields in the same table, for example : deleted_by, status... so, the question is, should excecute $post->delete(); and then $post->update( 'field' => 'value' ); or should I better create a method where I update all fields together ? something like this: $post->update([ 'status' => 'deleted', 'deleted_by' => $request->id_user, 'deleted_at' => Carbon::now() ]); also, what if I need to update other tables (cascade) but with the same idea (who deleted, status, deleted at)....should I better use transactions for this ? thanks a lot
How could we specify the total of withTrashed() can be recovered? For example, I would like to show only five items that were retrieve withTrashed, but all the data that is not deleted, should retrieve all of them.
Hi there, teacher. Nice video! i'm having a trouble, when i'm hitting the "restore" button or the "delete forever" button, it sends me right to the url, not to the function on my controller. I don't know what i did or what is happening, so, i need help there.
..... and what happens if i * create a record with title "abc and softdelete it * create a new one with the same title and softdelete it * create a new one with the same title and then softdelete it ... and then i want to go and restore all three softdeleted records .... which have the same title ... which is forbidden ofcourse ... (i know i can test it, but i just wanted to make the question since you didn't clarify that) thank you and that was a great video !!
I got one question, same as you show on the unique RULE part. Delete post "abc", and create another NEW post name "abc". What will happen if we restore the OLD "abc"?
CascadeSoftDeletes isn't an ideal solution, because it updates relationship objects one by one. I would rather use something that uses just the foreign key for building SQL query
Sir to use any css framework like css, why do we need to configure laravel mix and then compile the assets. Why dont we just copy the CDN link and paste on the html template
Thank you for your video Last time i used observables to handle soft delete relationships I want to know if there is a solution (package) can handle and archive images ?
You can do that with observers, too. Not sure about any package for specific images, maybe you can modify this one for your needs: github.com/spatie/laravel-directory-cleanup
Because i faced troubles with softdelete and observers, I use to do it like that public function deleting(Model $model) { if($model->isForceDeleting()) { event(new ModelDeleted($model)); } else { event(new ModelSoftDeleted($model)); } } Maybe a bad habit ?
I have a Parent, which is District , And District has child Division, ans aslo District has child Thana. That's mean Division ->District >Thana.. When i soft delete Division, the Thana is not deleting. Help me
many thanks for sharing this video, i just have question what if theres already existing database from client, and dev alter some columns inside migration script, does that break the records inside existing database? what is we think best practice on that kind of scenario?
Why do you think it would break anything? Databases gets updated all the time. And what you are asking is, what if I delete timestamps while the code uses Eloquent, well, give it a try and you will find out.
Sir Thank you very much for this tutorial and this is what I wanted. Sir please please make a video on the theme that you are using in PHP Storm. Your choice of theme is very good and I am unable to find the exact match with yours. Sir, please help me if possible to know me the theme name that you are using.
@@LaravelDaily Haven't you noticed that PHPstorm gives you error feedback while the code is actually valid? VSCode does the same. It's really confusing sometimes when the editor tells you you're wrong but you're not.
Oh I often ignore those, sometimes it's just some mis-caching when working with gazilion demo projects in my case. In other cases, I'm too lazy to install/configure something like IDE helper for a 5-minute demo.
@@LaravelDaily Even the core laravel functions like eloquent methods error, this has nothing to do with configuration. I'm not saying you need to do or change anything, I'm just pointing my frustrations with the error handling that is falling far behind the rapid changes in laravel. This started after laravel 7 was released. All the old packages won't give you any errors but all the new stuff does.
how can i do crud for api using try-catch block in controller for the resource route using route model binding Route::resource('articles', ArticleController::class, ['except' => ['create']]); here is my controller can you help me using try catch block in controller class ArticleController extends Controller { public function index() { return Article::all(); } public function show(Article $article) { return $article; } public function store(Request $request) { $article = Article::create($request->all()); return response()->json($article, 201); } public function update(Request $request, Article $article) { $article->update($request->all()); return response()->json($article, 200); } public function delete(Article $article) { $article->delete(); return response()->json(null, 204); } }