【问题标题】:Not able to restore soft deleted data in laravel无法在laravel中恢复软删除的数据
【发布时间】:2017-12-15 06:07:41
【问题描述】:

我想知道为什么我无法在 laravel 中恢复软删除的数据。我所做的一切都是正确的,但不确定我缺少什么,所以我无法恢复数据。

我的模特是

namespace App\Model\Clients;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class SocialAccounts extends Model{

   protected $table = 'social_accounts';
   use SoftDeletes;
   protected $dates = ['deleted_at'];
}

我使用了软删除特征。为了恢复我的软删除数据,我正在做类似下面的事情

$restoreAccount = SocialAccounts::withTrashed()->find($id)->restore();

但它不会恢复数据,我假设当我们恢复数据时 laravel NULL 删除了 deleted_at 列,但它没有更新该 id 上的任何内容

我也试过了

//second alternate method 
$restoreAccount= SocialAccounts::withTrashed()->find($id)->update(['deleted_at' => NULL]);

//Third alternate method
$restoreAccount = SocialAccounts::withTrashed()->find($id); 
$restoreAccount->deleted_at = NULL;
$restoreAccount->save();

我不确定我做错了什么,我们需要做些什么来实现恢复吗?我检查了官方 laravel 文档并遵循相同的。

【问题讨论】:

  • 您确定请求中的其他内容没有删除它吗?即它正在恢复但又被删除?
  • 我设法解决了。我正在使用数据库事务并且没有提交它我正在检查结果。傻我。感谢您的所有帮助。

标签: laravel eloquent soft-delete


【解决方案1】:

我不知道你是否已经找到答案,但也许你可以在你的控制器中试试这个

public function restore($id) 
{
    $restoreAccount = SocialAccounts::onlyTrashed()->->where('id', $id)->restore();
    return redirect()->route('your.route');
}

【讨论】:

    【解决方案2】:

    $restoreAccount = SocialAccounts::onlyTrashed()->find($id)->restore();

    【讨论】:

    • 不起作用,它返回 true 但仍然在 deleted_at 列中显示时间戳,当我选择数据时它还没有出现。
    猜你喜欢
    • 2020-07-29
    • 1970-01-01
    • 2019-01-25
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    相关资源
    最近更新 更多