Framework: [5.1] Soft deleted records are getting fetched.

Created on 1 Sep 2015  Â·  10Comments  Â·  Source: laravel/framework

deleted_at column is set when I delete a record and trashed() is returning true for soft deleted record. But when I get the result it contains soft deleted records.

Controller: index() function

  public function index()
    {

        $articles = Article::latest()->get();

        return view( 'articles/view_articles' )->with( [
            'articles'    => $articles
        ] );
    }

Model:

<?php namespace App\Models;

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

class Article extends Model
{
    use SoftDeletes;

    protected $fillable = [ 'name', 'url', 'description', 'status'];

    public $dates = [ 'deleted_at' ];

}

Migration:

 Schema::create( 'articles', function ( Blueprint $table ) {
            $table->mediumInteger( 'id', true, true );
            $table->string( 'name', 500 );
            $table->string( 'url', 500 );
            $table->text( 'description' );
            $table->timestamps();
            $table->softDeletes();
            $table->enum( 'status', [ 'Y', 'N', 'D' ] )->default( 'N' );
}

Most helpful comment

I just fixed the issue in our project.

Our models where extending an base model (which extends the Eloquent model). The base model has his own constructor, which didn't call the parent constructor.
So adding parent::__construct(); solved our problem.

All 10 comments

Duplicate.

If this issue is an duplicate, where is the other issue? I'm experiencing the exact same problem as @neowill2013 but can't find any info related to this issue.

+1 on this

+1 on this

I just fixed the issue in our project.

Our models where extending an base model (which extends the Eloquent model). The base model has his own constructor, which didn't call the parent constructor.
So adding parent::__construct(); solved our problem.

I just wasted any hour to realize it was a similar oversight as described by @mikevrind. Except in my case I was overriding the model's boot method without calling parent::boot();

@GrahamCampbell Still not fixed

This is not a bug, there is a possible cause of this issue in this thread.

Op 25 dec. 2015 om 18:35 heeft neowill2013 [email protected] het volgende geschreven:

Still not fixed

—
Reply to this email directly or view it on GitHub.

The fix or the correct way described by @mikevrind worked perfectly.
I had overridden boot and didn't call parent::boot(), so I was still getting the deleted rows, though deleted at was set. Once I called parent::boot() in the boot method it worked perfectly fine.

But I fail to understand the reason behind, when deleted_at was set without calling parent::boot() then what else is needed, can @mikevrind please explain. (sorry if its a novice question)

Thanks

Just dig in the code of the boot method to find out what is does once fired (and does't do when not fired ;) ).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JamborJan picture JamborJan  Â·  3Comments

YannPl picture YannPl  Â·  3Comments

RomainSauvaire picture RomainSauvaire  Â·  3Comments

ghost picture ghost  Â·  3Comments

PhiloNL picture PhiloNL  Â·  3Comments