51

in our project we must be use soft delete for each posts. in laravel document i think we can only use this feature for tables.

can we use that for posts on table such as

$id = Contents::find($id);
$id->softDeletes();

8 Answers 8

135

Updated Version (Version 5.0 & Later):

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Post extends Model {

    use SoftDeletes;

    protected $table = 'posts';

    // ...
}

When soft deleting a model, it is not actually removed from your database. Instead, a deleted_at timestamp is set on the record. To enable soft deletes for a model, specify the softDelete property on the model (Documentation).

For (Version 4.2):

use Illuminate\Database\Eloquent\SoftDeletingTrait; // <-- This is required

class Post extends Eloquent {

    use SoftDeletingTrait;

    protected $table = 'posts';

    // ...
}

Prior to Version 4.2 (But not 4.2 & Later)

For example (Using a posts table and Post model):

class Post extends Eloquent {

    protected $table = 'posts';
    protected $softDelete = true;
    
    // ...
}

To add a deleted_at column to your table, you may use the softDeletes method from a migration:

For example (Migration class' up method for posts table) :

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('posts', function(Blueprint $table)
    {
        $table->increments('id');
        // more fields
        $table->softDeletes(); // <-- This will add a deleted_at field
        $table->timeStamps();
    });
}

Now, when you call the delete method on the model, the deleted_at column will be set to the current timestamp. When querying a model that uses soft deletes, the "deleted" models will not be included in query results. To soft delete a model you may use:

$model = Contents::find( $id );
$model->delete();

Deleted (soft) models are identified by the timestamp and if deleted_at field is NULL then it's not deleted and using the restore method actually makes the deleted_at field NULL. To permanently delete a model you may use forceDelete method.

Sign up to request clarification or add additional context in comments.

4 Comments

The documentation has updated, the new answer by @majidarif should be accepted as correct
I've updated @AakilFernandes and it was given for prior to version 4.2 and which was right so I think I don't deserve a down vote for this answer but anyways :-)
Perhaps would be nice to reorder the answer's sections sorted by latest updates first? Seems a little archaic to have to scroll a ways through historical non-applicables. Thanks for the answer though, was exactly what I was looking for!
26

You actually do the normal delete. But on the model you specify that its a softdelete model.

So on your model add the code:

class Contents extends Eloquent {

    use SoftDeletingTrait;

    protected $dates = ['deleted_at'];

}

Then on your code do the normal delete like:

$id = Contents::find( $id );
$id ->delete();

Also make sure you have the deleted_at column on your table.

Or just see the docs: http://laravel.com/docs/eloquent#soft-deleting

5 Comments

then if how to delete normal posts in this model?
you can use $user->forceDelete();
my means is how to do not use SoftDelete? delete in normal mode delete from database. i think forceDelete() delete from Trash yes?
Laravel 4.2 uses Trait to soft delete records. The accepted answer is not working with Laravel 4.2.
Make sure to use Illuminate\Database\Eloquent\SoftDeletingTrait;
18

I just did this with Laravel 8 and it worked. It's basically what @The alpha said, but trying to wrap everything quicker. Follow this steps.

In the migration file add:

$table->softDeletes();

In the model:

use Illuminate\Database\Eloquent\SoftDeletes;

class User extends Model
{
    use SoftDeletes;
    ...
];

}

In the controller:

$user->delete();

Bonus: if you need to restore the deleted user

User::withTrashed()->find($id);->restore();

Comments

6

Just an update for Laravel 5:

In Laravel 4.2:

use Illuminate\Database\Eloquent\SoftDeletingTrait;    
class Post extends Eloquent {

    use SoftDeletingTrait;

    protected $dates = ['deleted_at'];

}

becomes in Laravel 5:

use Illuminate\Database\Eloquent\SoftDeletes;

class User extends Model {

    use SoftDeletes;
    protected $dates = ['deleted_at'];

Comments

4

In Laravel 5.5 Soft Deleted works ( for me ).

Data Base

deleted_at Field, default NULL value

Model

use Illuminate\Database\Eloquent\SoftDeletes;

class User extends Model {
    use SoftDeletes;
}

Controller

public function destroy($id)
{
    User::find($id)->delete();
}

Comments

3

In the Latest version of Laravel i.e above Laravel 5.0. It is quite simple to perform this task. In Model, inside the class just write 'use SoftDeletes'. Example

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class User extends Model
{
    use SoftDeletes;
}

And In Controller, you can do deletion. Example

User::where('email', '[email protected]')->delete();

or

User::where('email', '[email protected]')->softDeletes();

Make sure that you must have 'deleted_at' column in the users Table.

Comments

2

Here is the details from laravel.com

http://laravel.com/docs/eloquent#soft-deleting

When soft deleting a model, it is not actually removed from your database. Instead, a deleted_at timestamp is set on the record. To enable soft deletes for a model, specify the softDelete property on the model:

class User extends Eloquent {

    protected $softDelete = true;

}

To add a deleted_at column to your table, you may use the softDeletes method from a migration:

$table->softDeletes();

Now, when you call the delete method on the model, the deleted_at column will be set to the current timestamp. When querying a model that uses soft deletes, the "deleted" models will not be included in query results.

Comments

0

Updated Version (5.0 & later)

To enable this feature, we need to add a deleted_at column in our database (or migration) for mapping purposes.

For example:

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        ...
        $table->softDeletes(); // soft delete
    });
}

In addition, we need to use Illuminate\Database\Eloquent\SoftDeletes trait on the model

For example:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\SoftDeletes;

class User extends Model
{
    use SoftDeletes;

}

We can now call the delete() method as before

$user->delete();

For official documentation click here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.