Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if row is soft-deleted in Eloquent?

In Laravel 5.1 is there a nice way to check if an eloquent model object has been soft-deleted? I'm not talking about selecting data but once I have the object e.g. Thing::withTrashed()->find($id)

So far the only way I can see is

if ($thing->deleted_at !== null) { ... } 

I do not see any relevant method in the API that would allow for example

if ($thing->isDeleted()) { ... } 
like image 453
DisgruntledGoat Avatar asked Nov 30 '15 16:11

DisgruntledGoat


People also ask

How remove soft delete in Laravel?

The feature that you are looking for is soft-deleting in Laravel. You can simply add a new database column to your Laravel Model and use the SoftDeletes trait on your model. After that,you are good to go and soft deletes work instantly.

What is with trashed Laravel?

Laravel, “withTrashed()” linking a deleted relationshipIf the user gets deleted, and on the User model we use the SoftDeletes trait, you can use withTrashed() method here. class Post extends Model { public function user() {

What is soft delete in Laravel?

Soft deleting the data allows us to easily view and restore the data with minimal work and can be a huge time saver when data is accidentally deleted. Laravel provides support for soft deleting using the Illuminate\Database\Eloquent\SoftDeletes trait.

Is eloquent an ORM?

Eloquent is an object relational mapper (ORM) that is included by default within the Laravel framework. An ORM is software that facilitates handling database records by representing data as objects, working as a layer of abstraction on top of the database engine used to store an application's data.


1 Answers

Just realised I was looking in the wrong API. The Model class doesn't have this, but the SoftDelete trait that my models use has a trashed() method.

So I can write

if ($thing->trashed()) { ... } 
like image 192
DisgruntledGoat Avatar answered Sep 24 '22 02:09

DisgruntledGoat