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 ?
<?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
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;
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
SoftDeletestrait didn't boot which means that the entire soft deleting functionality doesn't work with that code.