What is soft delete in Laravel?

Full Stack Developer with a passion for building web applications. PHP, Node.js, Laravel, MySQL, MongoDB. Love collaborating & making a difference
Search for a command to run...

Full Stack Developer with a passion for building web applications. PHP, Node.js, Laravel, MySQL, MongoDB. Love collaborating & making a difference
Add it to the boot method of the AppServiceProvider of the app to improve Laravel developmet experience. use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\DB; class AppServiceProvider extends ServiceProvider { //... pub...

There is a plugin available for reactjs that one can use to add Calendly to Nextjs i.e., react-calendly. The problem is if you want to customize the button to open popup or modal then you need to have Calendly's pro plan for it. Here is the alternati...

After shifting to Ubuntu from Windows, I constantly look up for the alternatives of the tools that are available in Windows to enhance my arsenal in Linux. DbGate is the smartest SQL+noSQL database client. It works on Windows, Linux, MacOS and in web...

<div className="ratio ratio-16x9"> <video width="900" height="650" muted playsInline autoPlay loop> <source src="/images/test.mp4" type="video/mp4" /> </video> </div>

Creating middleware in Laravel is a common task that helps to filter HTTP requests entering your application. Middleware can perform various tasks such as authentication, logging, and modifying request/response objects. Step 1: Create Middleware You ...

In Laravel, soft delete is a feature that allows you to "delete" a record from a database table without actually removing it physically. Instead of permanently deleting the record, a timestamp is set on a special "deleted_at" column, indicating when the record was soft deleted. This allows you to retain information about the deleted records and, if necessary, recover them later.
To implement soft deletes in Laravel, you typically follow these steps:
softDeletes method in your migration file:Schema::table('your_table', function ($table) {
$table->softDeletes();
});
Model Configuration: In your Eloquent model, use the SoftDeletes trait, which is included with Laravel:
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class YourModel extends Model
{
use SoftDeletes;
}
Usage: Now, when you delete a record using Eloquent, it will be a soft delete, and the "deleted_at" column will be set to the current timestamp:
YourModel::find($id)->delete();
To retrieve soft-deleted records, you can use the withTrashed method:
$softDeletedRecords = YourModel::withTrashed()->get();
To only retrieve soft-deleted records, you can use the onlyTrashed method:
$softDeletedRecords = YourModel::onlyTrashed()->get();
To restore a soft-deleted record, you can use the restore method:
YourModel::withTrashed()->find($id)->restore();
To permanently remove soft-deleted records, you can use the forceDelete method:
YourModel::withTrashed()->find($id)->forceDelete();
Soft deletes are useful in scenarios where you want to keep a record of deleted items for audit purposes or in case you need to recover deleted data.