Framework: [L5.6] User model use __construct() and softdelete

Created on 1 Sep 2018  路  4Comments  路  Source: laravel/framework

  • Laravel Version: 5.6
  • PHP Version: 7.1.7
  • Database Driver & Version: 10.1.25-MariaDB

Description:

Model User use softdelete ( row data field deleted_at is not null ) the data not come out
when Model User use softdelete and public function __construct() ( row data field deleted_at is not null ) the data come out

how to solve model user use function __construct() and softdelete to make data not come out ?

Steps To Reproduce:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;

class User extends Authenticatable
{
    use Notifiable;

    use SoftDeletes;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    protected $dates = ['deleted_at'];

}

$user = User::get();
print_r( $user ) // data ( deleted ) not come out

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;

class User extends Authenticatable
{
    use Notifiable;

    use SoftDeletes;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    protected $dates = ['deleted_at'];

    public function __construct() {}

}

$user = User::get();
print_r( $user ) // data ( deleted ) come out

Most helpful comment

The reason you get the soft deleted user in the second example is because you defined a constructor in your model and you didn't call the parent constructor so the SoftDeletes trait didn't boot which means that the entire soft deleting functionality doesn't work with that code.

All 4 comments

The reason you get the soft deleted user in the second example is because you defined a constructor in your model and you didn't call the parent constructor so the SoftDeletes trait didn't boot which means that the entire soft deleting functionality doesn't work with that code.

Okay, It works. Thanks, @X-Coder264 .

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;

class User extends Authenticatable
{
    use Notifiable;

    use SoftDeletes;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    protected $dates = ['deleted_at'];

    public function __construct() {

        parent::__construct();

    }

}

this should be closed. Also ... I would kindly suggest to post things like this in the laracasts forum for example. Regards

@taylorotwell , @GrahamCampbell , @gzai please close this issue, since the problem is fixed;

Was this page helpful?
0 / 5 - 0 ratings