In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "case analysis of soft deletion in laravel". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this article "case analysis of soft deletion in laravel" can help you solve the problem.
In laravel, soft deletion means that the table record is not actually deleted from the database, but the representation status of the table record is marked as soft delete, so that it can be filtered at query time to make the corresponding table record look like it has been "deleted".
This article operating environment: Windows10 system, Laravel6 version, Dell G3 computer.
What is the principle of soft deletion in laravel
1. Delete the model
1.1 use delete to delete the model
It is easy to delete the model, first get the model instance to be deleted, and then call the delete method:
$post = Post::find (5); if ($post- > delete ()) {echo 'deleted article successfully!' ;} else {echo 'failed to delete the article!' ;}
The method returns true or false.
1.2 use destroy to delete the model
Of course, if you know the model id you want to delete, you can delete it directly by using a simpler method, destroy:
$deleted = Post::destroy (5)
You can also import more than one model at a time, id, and delete multiple models:
$deleted = Post::destroy ([1, 2, 3, 4, 5])
Call the destroy method to return the number of records that have been deleted.
1.3 use query Builder to delete the model
Since we mentioned earlier that the Eloquent model itself is a query builder, we can also delete the model using the query builder style. For example, if we want to delete all articles with zero views, we can use the following ways:
$deleted = Models\ Post::where ('views', 0)-> delete ()
Returns the number of articles whose result is deleted.
2. Soft deletion and its related implementation
2.1 soft deletion implementation
All of the above deletion methods delete data table records from the database, and the Eloquent model also supports soft deletions.
The so-called soft deletion means that the data table record is not really deleted from the database, but the identification status of the table record is marked as soft deletion, so that it can be filtered when querying, so that the corresponding table record appears to be "deleted". A date field is used as the identification status in Laravel. This date field can be customized. Here we use deleted_at. If the corresponding model is soft deleted, the value of the deleted_at field is the deletion time, otherwise the value is empty.
For the Eloquent model to support soft deletion, some settings need to be made. First, you need to use SoftDeletestrait in the model class. The trait provides a series of related methods for soft deletion. For more information, please see the source code Illuminate\ Database\ Eloquent\ SoftDeletes. In addition, set the $date attribute array and put deleted_at in it:
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.