我有一个带有softdeletes的模型,如果我添加一个追加字段,我的sofdeletes实现不起作用,有人知道发生了什么吗?
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Users extends Model
{
use SoftDeletes;
protected $dates = ['deleted_at'];
protected $table = 'users';
protected $appends = ['assigned'];
protected $fillable = ['category_id'];
public function __construct()
{
$this->assigned = false;
}
public function setAssignedAttribute($value)
{
$this->attributes['assigned'] = $value;
}
public function getAssignedAttribute($value)
{
return $value;
}
}
答案 0 :(得分:1)
这不是你的追加领域。你的构造函数没有调用父构造函数,这是启动你的软删除特征的东西。在构造函数中添加parent::__construct()
。它应该匹配父母的签名,所以它应该是:
public function __construct(array $attributes = [])
{
$this->assigned = false;
parent::__construct($attributes);
}
答案 1 :(得分:0)
您误读了文档,您的访问者没有正确的名称:
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Users extends Model
{
use SoftDeletes;
protected $dates = ['deleted_at'];
protected $table = 'users';
protected $appends = ['assigned'];
protected $fillable = ['category_id'];
public function __construct()
{
$this->assigned = false;
}
public function setIsAssignedAttribute($value)
{
$this->attributes['assigned'] = $value;
}
public function getIsAssignedAttribute($value)
{
return $value;
}
}
您需要Is
,即使它不是变量名称的一部分。