Digging into Laravel SoftDeletes

Laravel SoftDeletes
public function apply(Builder $builder, Model $model)
{
$builder->whereNull($model->getQualifiedDeletedAtColumn());
}
const DELETED_AT = ‘your_column_name’;
protected function addWithTrashed(Builder $builder)
{
$builder->macro(‘withTrashed’, function (Builder $builder, $withTrashed = true) {
if (! $withTrashed) {
return $builder->withoutTrashed();
}
return $builder->withoutGlobalScope($this);
});
}
//This will call the Delete Method in Trait
Post::find(2)->delete()
//This will call the Delete Method in Scope.
Post::withoutTrashed()->delete()

--

--

--

Freelancer Developer.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How do you get the maximum and minimum values for integer data types based on the operating system?

Creating a new scripting language on PHP

CockroachDB vs. Postgres: An Open Source Showdown

Apache Spark RDD

Talk to your Smart Home via SEPIA Open Assistant

Discord ConnectX

How I solved Dynamic Task Scheduling using AWS DynamoDB TTL, Stream and Lambda

Simple Login and Signup in Rails

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Saurabh Mahajan

Saurabh Mahajan

Freelancer Developer.

More from Medium

Using Database Transactions in Laravel

Dockerize Laravel & WordPress with— MailHog, MySQL, PHP, Node

What’s new in Laravel 8

Laravel Illuminate\Contracts\Container\BindingResolutionException