Eloquent Soft Deletes: Things You May Not Know

  Рет қаралды 21,564

Laravel Daily

Laravel Daily

Күн бұрын

Пікірлер: 99
@ricko13
@ricko13 3 жыл бұрын
You just cover EVERYTHING about soft deletes in just 10 minutes 👏 thumbs up!
@o_lobato
@o_lobato 3 жыл бұрын
what strategies could be used to restore an old post if there is a newer with same title?
@ricko13
@ricko13 3 жыл бұрын
@@o_lobato you can also use the soft deletes as an "archive" so you can recover them later, watch the video again
@mohamedsiddig1925
@mohamedsiddig1925 3 жыл бұрын
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.
@nirajgautam403
@nirajgautam403 3 жыл бұрын
I already knew all this things.. but still enjoyed watching it.
@yungifez
@yungifez 3 жыл бұрын
Its crazy how simple laravel makes soft deletes
@dex.cabigting
@dex.cabigting 3 жыл бұрын
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)
@Somcoders
@Somcoders 3 жыл бұрын
Thanks Povilas. skipping unique validations may couse an error when restoring.
@abdulrehmandar8541
@abdulrehmandar8541 3 жыл бұрын
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?
@LaravelDaily
@LaravelDaily 3 жыл бұрын
It's personal preference
@ShroomMusicChannel
@ShroomMusicChannel 3 жыл бұрын
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"?
@LaravelDaily
@LaravelDaily 3 жыл бұрын
Both will be active, then, you need to have a separate code to avoid it.
@rossco5551
@rossco5551 3 жыл бұрын
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).
@JayEdgarT
@JayEdgarT 2 жыл бұрын
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.
@sasocvetanoski9760
@sasocvetanoski9760 2 жыл бұрын
Thank you for sharing your experience 🍻
@mhmdomer
@mhmdomer 3 жыл бұрын
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();
@alnahian2003
@alnahian2003 2 жыл бұрын
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!
@yordanmilchev
@yordanmilchev 6 ай бұрын
Best person I know on this planet
@mohammad_alhalla30
@mohammad_alhalla30 Жыл бұрын
thanks for your time , جزاك الله خيرا
@MirzaBilal1992
@MirzaBilal1992 3 жыл бұрын
Can we restore all of the deleted posts on restoring the Author as well?
@camilooyarzo9987
@camilooyarzo9987 3 жыл бұрын
hi i need to install jetstrap with bootstrap 4 but now by default it uses bt 5 how can i change it?
@PovilasKorop
@PovilasKorop 3 жыл бұрын
Read the documentation of Jetstrap.
@michaelcrash
@michaelcrash 3 жыл бұрын
my advise on cascade softdelete is to use build-in model events. "deleting" in your model method booted() to handle softdelete of child/parent models.
@bariqdharmawan4680
@bariqdharmawan4680 3 жыл бұрын
you mean using something like UserObserver and when user is deleted, perform a soft delete too to post right?
@robertomejia3124
@robertomejia3124 2 жыл бұрын
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
@frtrash766
@frtrash766 2 жыл бұрын
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.
@madrise007
@madrise007 2 жыл бұрын
when should use Soft Deletes laravel? I read that some people said that soft deletes is bad practice.
@alex_nita
@alex_nita 2 жыл бұрын
Great tip/tutorial. Thank you! P.S. what is the DB app used by you (in the video)
@LaravelDaily
@LaravelDaily 2 жыл бұрын
Table plus or sequel pro, I don't remember which
@alex_nita
@alex_nita 2 жыл бұрын
@@LaravelDaily thank you
@jessedeboer2301
@jessedeboer2301 3 жыл бұрын
What happens when you restore the user? are the posts automatically restored to?
@ossamakhayat5390
@ossamakhayat5390 3 жыл бұрын
Clear and informative as usual. Thanks!
@localLyricsPh
@localLyricsPh Жыл бұрын
how did you setup the editor to have keywords like "uri", "relations", "key", and "var_name" etc.
@LaravelDaily
@LaravelDaily Жыл бұрын
Phpstorm by default
@vakylenkox
@vakylenkox 3 жыл бұрын
Short format for rule: 'title' => 'unique:posts,deleted_at,NULL',
@rs-nm7hp
@rs-nm7hp 3 жыл бұрын
Yeah .. i always implement this method not Rule object
@vinsmoke.sanji.4
@vinsmoke.sanji.4 3 жыл бұрын
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 ?
@LaravelDaily
@LaravelDaily 3 жыл бұрын
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
@vinsmoke.sanji.4
@vinsmoke.sanji.4 3 жыл бұрын
@@LaravelDaily thank you for your time boss Warm regards
@JovenAlbarida
@JovenAlbarida 3 жыл бұрын
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?
@BsiennKhan
@BsiennKhan 3 жыл бұрын
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.
@LaravelDaily
@LaravelDaily 3 жыл бұрын
I'm planning to shoot a video on that exact scenario, next week.
@frank_core3193
@frank_core3193 3 жыл бұрын
is there a way where we can fetch all the soft deleted record... when i say all i mean all tables like post,users,conments...
@LaravelDaily
@LaravelDaily 3 жыл бұрын
The model structure is different for each model so how can you group them together? Query each model separately.
@frank_core3193
@frank_core3193 3 жыл бұрын
@@LaravelDaily Owkay... makes a lot of sense
@erlonpaimsodre6506
@erlonpaimsodre6506 2 жыл бұрын
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.
@rjchhetri2370
@rjchhetri2370 3 жыл бұрын
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
@LaravelDaily
@LaravelDaily 3 жыл бұрын
You can use CDN, if the theme provides the CDN links
@mangoknights
@mangoknights 3 жыл бұрын
Hi. By chance you offer talent/project staffing?
@LaravelDaily
@LaravelDaily 3 жыл бұрын
Nope, sorry
@TahirBhai
@TahirBhai 3 жыл бұрын
Thanks for sharing it, I have learned new package today.
@masedinet
@masedinet 3 жыл бұрын
Nice tutorial using softdelete sir,
@arifulsikder777
@arifulsikder777 2 жыл бұрын
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
@LaravelDaily
@LaravelDaily 2 жыл бұрын
Here's my article about it: blog.quickadminpanel.com/one-to-many-with-soft-deletes-deleting-parent-restrict-or-cascade/ - read section "Behavior 3"
@KevinYobeth
@KevinYobeth 3 жыл бұрын
What about restoring soft deleted child? Is there a way to restore the child after restoring the parent itself?
@LaravelDaily
@LaravelDaily 3 жыл бұрын
Googled this: stackoverflow.com/questions/61245181/restore-soft-delete-child-in-laravel
@SanjaHD
@SanjaHD 3 жыл бұрын
Does the package offer also cascade restore?
@LaravelDaily
@LaravelDaily 3 жыл бұрын
No
@julienSibille
@julienSibille 3 жыл бұрын
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 ?
@LaravelDaily
@LaravelDaily 3 жыл бұрын
I would probably try to debug what troubles you had exactly and fix the actual issue, instead of this workaround. But it works!
@gekost79
@gekost79 3 жыл бұрын
..... 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 !!
@LaravelDaily
@LaravelDaily 3 жыл бұрын
Well it's forbidden for a reason - so you can restore only one of the records, other ones would throw error.
@marcinrobertkazmierczak
@marcinrobertkazmierczak 2 жыл бұрын
What with pivot table and soft deletes. How this things gonna work with soft deletes?
@LaravelDaily
@LaravelDaily 2 жыл бұрын
I wouldn't personally use soft deletes in a pivot table. But yes, you can do it if you create a Model for the pivot table.
@marcinrobertkazmierczak
@marcinrobertkazmierczak 2 жыл бұрын
@@LaravelDaily thank you for quick response
@marcinrobertkazmierczak
@marcinrobertkazmierczak 2 жыл бұрын
@@LaravelDaily what if I need to save data form pivot as softdeleted? Only manual manipulation with deleted_at field in pivot table?
@LaravelDaily
@LaravelDaily 2 жыл бұрын
Yes, from what I remember
@marcinrobertkazmierczak
@marcinrobertkazmierczak 2 жыл бұрын
@@LaravelDaily I found package for this on git hub and I'm testing this solution right now.
@kingstalker
@kingstalker Жыл бұрын
what if you want to restore it with the same title but now the title already got created by a new record
@LaravelDaily
@LaravelDaily Жыл бұрын
Then you're screwed? :)
@bestwishes3040
@bestwishes3040 3 жыл бұрын
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
@LaravelDaily 3 жыл бұрын
Material darker
@bestwishes3040
@bestwishes3040 3 жыл бұрын
@@LaravelDaily Thank you Sir
@baldomwamba
@baldomwamba 8 ай бұрын
How to soft delete the pivot table ?
@LaravelDaily
@LaravelDaily 8 ай бұрын
You can't in Laravel, unfortunately, at least not directly. You need to build that functionality manually if you really want it.
@Stoney_Eagle
@Stoney_Eagle 3 жыл бұрын
What's up with all the false positive errors lately in the IDE's for laravel?
@LaravelDaily
@LaravelDaily 3 жыл бұрын
M?
@Stoney_Eagle
@Stoney_Eagle 3 жыл бұрын
@@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.
@LaravelDaily
@LaravelDaily 3 жыл бұрын
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.
@Stoney_Eagle
@Stoney_Eagle 3 жыл бұрын
@@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.
@LaravelDaily
@LaravelDaily 3 жыл бұрын
So your suggestion is... to not release any new features in Laravel?
@charitysimon-peraboh5555
@charitysimon-peraboh5555 3 жыл бұрын
Thanks so much
@tastytim
@tastytim 2 жыл бұрын
thank you
@Kanexxable
@Kanexxable 3 жыл бұрын
People who try to use eloquent often say that it's a slow performance orm can you show people how to do performant queries and read writes.
@LaravelDaily
@LaravelDaily 3 жыл бұрын
Yes I'm planning a new course about Eloquent performance, will be published by the end of October, on laraveldaily.teachable.com
@Kanexxable
@Kanexxable 3 жыл бұрын
@@LaravelDaily Good I would like to see previews
@ekponoambrose1909
@ekponoambrose1909 3 жыл бұрын
Thank you
@christian-ngabe
@christian-ngabe Жыл бұрын
Let me say thanks so much
@er.anilkumarthakur1493
@er.anilkumarthakur1493 3 жыл бұрын
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); } }
@LaravelDaily
@LaravelDaily 3 жыл бұрын
Try to search "try catch" on my channel and you will find a lot of video examples
@codepopular
@codepopular 3 жыл бұрын
Thanks.
@GergelyCsermely
@GergelyCsermely 3 жыл бұрын
Thanks
@Formula7Driver
@Formula7Driver 3 жыл бұрын
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
@RaajKhan
@RaajKhan 3 жыл бұрын
Thanks
Faster Eloquent: Avoid Accessors with Foreach
9:35
Laravel Daily
Рет қаралды 54 М.
Eloquent Performance: TOP 3 Mistakes Developers Make
7:59
Laravel Daily
Рет қаралды 50 М.
Quando A Diferença De Altura É Muito Grande 😲😂
00:12
Mari Maria
Рет қаралды 45 МЛН
Should you Delete or Soft Delete?
6:38
CodeOpinion
Рет қаралды 11 М.
Laravel 6 Advanced - e9 - Soft Deletes
12:17
Coder's Tape
Рет қаралды 26 М.
What Senior Devs ACTUALLY Do? (And how to become one)
7:12
Laravel Daily
Рет қаралды 6 М.
How to use soft deletes in Laravel
10:35
Andrew Schmelyun
Рет қаралды 8 М.
Laravel Security: Top 7 Mistakes Developers Make
11:16
Laravel Daily
Рет қаралды 88 М.
Laravel Junior Code Review: 12 Tips on Everything
15:30
Laravel Daily
Рет қаралды 76 М.
This is apparently the most loved CRUD app of 2023
19:38
Dreams of Code
Рет қаралды 131 М.
Solving one of PostgreSQL's biggest weaknesses.
17:12
Dreams of Code
Рет қаралды 226 М.
Exceptions in Laravel: Why/How to Use and Create Your Own
12:18
Laravel Daily
Рет қаралды 89 М.
5 deadly Rust anti-patterns to avoid
13:25
Let's Get Rusty
Рет қаралды 40 М.