Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all rows (soft deleted too) from a table in Laravel?

To get all rows from a table, I have to use Model::all() but (from good reason) this doesn't gives me back the soft deleted rows. Is there a way I can accomplish this with Eloquent?

like image 267
totymedli Avatar asked Dec 09 '13 15:12

totymedli


People also ask

How do I remove all records from a table in Laravel eloquent?

Our records will be automatically deleted from the cache if users delete all the records from the table. But we can delete it from the cache only if we are using the model of Laravel Eloquent. In the first example, we are going to use the truncate() function, which is used to delete all the records.

What is soft deleting 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.


2 Answers

To also get soft deleted models

$trashedAndNotTrashed = Model::withTrashed()->get();

Only soft deleted models in your results

$onlySoftDeleted = Model::onlyTrashed()->get();
like image 63
marcanuy Avatar answered Oct 19 '22 17:10

marcanuy


Use this to get all record

Model::withTrashed()->get();

Use this to get record of particular id

Property::withTrashed()->find($list->property_id);
              or

// 1 is unique id of the table

 Model::withTrashed()->find(1);
like image 37
kush Avatar answered Oct 19 '22 19:10

kush