Laravel 6 Advanced
Lección 9 de 10 · Laravel 6 Advanced – e9 – Soft Deletes · Educación
Laravel 6 Advanced – e9 – Soft Deletes
[Music] welcome back to level six advanced in this episode we're going to start talking about soft delete now soft
deletes is a very nice built-in level feature that allows us to basically hide records as opposed to completely deleting them from the database now the way that this actually works is using a
timestamp column called deleted at and if that is not equal to no then that means that this record was soft deleted now use cases of this are going to depend on your specific application but
it's a very nice way of keeping records of things like users for example imagine a user may be deleted their account but you don't want to lose all records of that user you simply want temporarily
hide their account if you want to think of it that way you can always bring back that account if you wanted to or whatever other record you are talking about and that's the nice thing with
soft deletes I've done a little bit of homework for this episode and I've set up a very simple post all I have is simply a post model in which I've turned guarded off I've also got a migration
for a create post table which just simply has a title and a body as well as a factory so a post factory and here it is now in terms of my controller I've got a post controller which in the index
method all it's doing is just returning all posts and then in the browser you'll notice we have a slash post route setup and this is actually completely empty right now
I don't have anything in the database we're actually using SQLite for this episode but you can use just about any database that you would like alright with that being said we need to add some
records into this database now we do have a factory so let's use the factory for that let's jump into PHP artisan tinker and run the following will say PHP artisan tinker use a factory to
create some app post class let's create maybe ten of them will persist them to the database with create okay now if we hit refresh sure enough we get that in here it doesn't look very
pretty but if you're using Chrome you can actually download a JSON plugin that you can use is called JSON format err and we'll just look up for Chrome and there it is
and JSON format err will actually nicely format things like this into a more readable way so we'll add that to my Chrome as an extension and with that added in I'll give this a refresh and
there we go much nicer right that's JSON format er there are a ton of different JSON formatting extensions that you can use that's just one of them this is the one that I typically use all right so
much easier to see now that we have this now I could go back into my database and change the migration you would know how to do that but I would lose all of my records so for this example I'm actually
going to show you how to create a complimentary migration this is one of those tricks that you pick up along the way whenever you've got some data already in production and of course you
need to add this new deleted add column in order for this to work so let me show you how to actually do that I'll exit out a tinkerer and we'll say a PHP artisan make migration and we'll call
this one add deleted at column two posts now we can pass a flag of – – table and we could tell at what table we want the table that we want in this case is the post table this is going to pre-fill a
couple of things for us so let's jump back to phpstorm and take a look I had to leave that column here enough you see here that we have posts pre-populated for us in our schema so what we need to
do here is go ahead and grab our table and this is still the blueprint and on this one let's go ahead and add a new timestamp called delete it at and this one needs to be nullable and the reason
for this is that any records that are not active will have a deleted add column of null that means that it's not deleted however when you do delete it all you're doing is setting this deleted
add timestamp now don't worry delever actually does that for us we're not actually going to do it now on our down method if you write down methods these are optional but it's nice to always
write them from time to time so say table drop column delete that but now we can migrate our database again with PHP artisan migrate and there
we go so now we've added that deleted add call now we hit refresh here the leader that is no but now if we wanted to make a change for example we can actually revert that back we can say PHP
artisan migrate rollback and this will actually give us an error and it might be a little bit confusing at first as to this but if you actually look at the documentation it warns you about this
let's take a look we jump into the level documentation let's go ahead and put drop column right here and there we go so to drop a column we actually require a special package and it'll tell us all
about it right here and it's this doctrine package and it tells you you will need to add it if you are using SQLite now this is particularly to me because I am using SQLite and actually
one of the reasons why I did was because I wanted to show you about this doctrine package very simple to fix we'll just do composer require will require that package in alright so now that we have
this package let's go ahead and try this one more time we'll go ahead and try to roll back our database and this time it does work now the nice thing with this is now our data is still there but we
don't have that deleted that column migrated again of course that'll work now we have our deleted column back into our table now if you wanted to move this deleted at column
after the body you actually could however it wouldn't work in SQLite it really only works in other database drivers but what you would do is you would say add my
timestamp but then I want you to add it after and then you say a column name in this case I would say after body right and that would theoretically put it after this I will leave that in the code
however even if I migrate it rollback and went back in it wouldn't work because the way SQLite works it doesn't respect that command it would just throw that at the end anyway so it wouldn't
really work however it's a nice feature to know because in most scenarios it is nice to have the time steps at the very end so whenever you add any columns I always use the after so that might
regular update it and created at timestamps are always on the right just a little convention that I follow okay so with this we're almost done with the setup back in the model the only thing
that I need to do is act to the use soft elites so I could say use soft deletes and that is located in illuminate database eloquent and it's called soft deletes
okay notice that it did get imported up here at the top so with this that has basically done everything that we need at that point and to prove it of course we come back here we still have all ten
of our records however let's pick the record with ID of one and let's try to soft delete that one record alright so we could do that using tinker will say PHP artisan tinker
we'll say give me my post from app post find any idea of one and there it is and then I can say post delete just a regular delete that you would use right nothing crazy now if we come back here
we hit refresh notice ID of one is gone we started with 2 3 4 8 9 10 and there is no ID of 1 however that is still in our database and I can prove it like so we'll go ahead and fetch that post again
and now it says no right it didn't find it interesting let's rewrite this a little bit that way we can find it will say where ID equals one get do you think that this is
gonna work nope it doesn't work okay so we need one magic word in here and that is with trashed okay and sure enough there it is so if you add the width trashed you are able to access
things that have been soft deleted but notice that all of my regular things that I would normally use such as a find and a wear statement those don't work those returned either no or an empty
collection or something like that soft deletes basically act exactly as if you had deleted the record the record is still in your database though and so the nice thing is that if we add with
trashed in this case we do get back this record and so we have that right here but again just to recap it doesn't appear here you can use where statements you can use your regular relationships
that you used to you could do everything exactly the same except the records are still in there so how would we bring this record back well truthfully all you would really need to do is set this back
to no but of course laravel has a nice way for us to do that so what I will actually do is go ahead and use the first one there we go that way I actually have the post and not just a
collection and so with my post I can say restore and there we go it gave me a restore of true and if I hit refresh there is ID of one now here's another little thing that
you can do let's go ahead and create a quick policy will save PHP artisan make policy post policy and if we open this up it's actually empty let's do something let me redo
that I will actually delete this file let's go back into here and if you actually remember we can actually pass in a model let me show you will say help and there we go
so the – M flag will allow us to pass in a model that way everything gets filled out for us don't say – M for post and this time there we go and this time we do have a
nice and full policy now in policies check this out here's restore and here's force delete so these are two things that we need to talk about w haven't quite talked about
we did talk about the restore and if you are using a policy maybe restore can only be done by administrators in that case of course you would do something like restore is only allowed if the user
maybe is administrator this doesn't exist this is just code doesn't really exist just an example of something you can do because maybe your typical users they can delete records but they can't
restore a record because in a way that's almost like bringing a record back from the dead a lot of times you may not want just a regular user to be allowed to do that maybe only administrators can
actually restore a dead record finally there is a force delete and as the name would suggest force delete is a true delete this will completely kill that record off the database not soft delete
it but really get rid of it the way a normal delete call would work so you still allow to permanently delete a record it just doesn't work the way that you would intend it to so how will
we use force delete well let's jump back into tinker so I can show you that and we'll go ahead and grab that post again and there it is we'll say with trash and give me the first one and there it is
now at this point deleted that is set to know if I really truly wanted to delete this but I could do is say post force delete and that equals to true and now even if
I try to bring that post back it's gone that's really gone this is not soft deleted at this point using force delete does delete the record off the database 100% and if we head back into here again
part of the policies you have this restore and you do have these force delete so whenever you're generating your policies and using soft deletes at the same time this is the way that you
restrict who actually gets to permanently delete records and who actually gets to restore a record that has been soft deleted nice and easy to do with policies we've talked about
policies if you have any questions at all in generating policies for a model check out the policies video it's very thorough we're not gonna get into a lot of details with the policy because we've
already covered that so with that we'll wrap it up for this lesson on soft deletes I hope you find good use for soft deletes I've used them a handful of times sometimes it's exactly what you
need and as always my name is Victor don't forget to subscribe and thanks for watching
At times, you need your application to keep records in the database but still make them disappear from functionality. That’s where soft deletes come into play. They allow you to complete hide a record from the database without actually deleting the record. Let’s take a look at an example.
For the best experience, follow along in our interactive school at https://www.coderstape.com
Documentation
https://laravel.com/docs/6.x/eloquent#soft-deleting
Policy Video
DigitalOcean Referral
https://m.do.co/c/7dce5364ef4d
Hit us up on Twitter with any questions or comments @codertape (https://twitter.com/CodersTape)
About This Course
Ready to level up your Laravel skills? This series is for you, follow along as we uncover the advanced Laravel features that actually make Laravel the most popular php framework. We’ll have lot of tips and tricks along the way. Are you ready?