我想为用户可以上传的图片创建删除功能。图片类的模型:删除on Eloquent不删除Laravel 5.4
class Picture extends Model
{
use SoftDeletes;
/**
* The attributes that are mass assignable.
* @var array
*/
protected $fillable = ['original_file', 'resized_file'];
/**
* The attributes that should be mutated to dates.
* @var array
*/
protected $dates = ['deleted_at'];
/**
* Get the category that owns the question.
*/
public function user()
{
return $this->belongsTo(User::class);
}
迁移:
$table->timestamps();
$table->softDeletes();
删除功能,我有:
public function destroyProgress(Picture $progress){
$progress->delete();
dd($progress);
return view('client.home.profile', ['user' => Auth::user()]);
}
该代码被执行,并在采空dd($progress);
。我预计这将返回null
,因为$进度已被删除。但是这仍然会返回一个实例!有人可以向我解释我做错了什么,因为我失去了言语。
的'delete'方法应该返回TRUE;或'FALSE'这样你就可以检查是否删除成功与否。但在内部,我非常肯定模型只是将'exist'属性设置为false,以表明该模型不再在数据库中表示,但模型本身仍然完好无损。我会检查返回值'$ deleted = $ progress-> delete();'看看删除是否成功,然后如果是这样,就忘记模型。 – Jonathon
@Jonathon谢谢你的回答。我不认为我在我的问题中表达了这一点,但数据库中的deleted_at属性没有改变。我添加了'$ deleted',并用dd返回'null'。我也在'$ progress-> delete()'后面尝试了'dd($ progress-> deleted_at)',这也返回null。 –
你试过类似$ pic = Picture :: findOrFail($ id)的东西吗? $ pic-> delete()? – sam12