Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Laravel中还原已删除通知时出现问题_Php_Laravel - Fatal编程技术网

Php 在Laravel中还原已删除通知时出现问题

Php 在Laravel中还原已删除通知时出现问题,php,laravel,Php,Laravel,我正在尝试使用Laravel通知还原已删除的通知。我遇到的问题是我犯了错误 Call to undefined method Illuminate\\Notifications\\Notification::withTrashed() 因为通知模型在供应商文件夹中,我无法更改它。所以我需要一些解决方法,以便withTrashed方法在通知模型中可用。感谢您的帮助。这是我的密码 控制器 public function restoreDeletedNotification(Request $req

我正在尝试使用Laravel通知还原已删除的通知。我遇到的问题是我犯了错误

Call to undefined method Illuminate\\Notifications\\Notification::withTrashed()
因为通知模型在供应商文件夹中,我无法更改它。所以我需要一些解决方法,以便withTrashed方法在通知模型中可用。感谢您的帮助。这是我的密码

控制器

public function restoreDeletedNotification(Request $request)
{
    $restore = Notification::withTrashed()->where('id', $request['id'])->restore();

    return response()->noContent();
}
web.php

Route::post('/notifications/restore', [\App\Http\Controllers\NotificationController::class, 'restoreDeletedNotification'])->name('restore-notification');

首先请注意:

  • 默认情况下,Laravel通知的迁移文件在处没有一个
    deleted\u
  • 您正在调用
    illumb\\Notifications\\Notification
    ,它不扩展
    Model
    类,因此没有
    withTrashed()
    方法
解决方案:

我们要做的是解决问题。您需要创建一个扩展
模型的
通知
模型(在
应用程序
目录中或
应用程序/模型
如果您使用的是Laravel 8),然后您将使用
软删除
特性

class Notification extends Model
{
    use SoftDeletes;

     /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = ['deleted_at'];
}
现在,您需要创建一个迁移文件,在
通知
表的
列中添加
deleted\u,如下所示

  • 运行php artisan make:migration AddDeleteDatColumntNotificationsTable
  • 在迁移文件中,您将添加
  • 运行php artisan迁移

  • 现在您可以使用
    App\Notification::withTrashed()->where('id',$request['id')->restore()

    通知不会扩展模型,因此没有类似的withTrashed或任何其他模型查询生成器内容。也会触发通知,请执行此操作。。。就这样,我认为DB在这里和任何被删除的地方都不相关。你为什么认为你可以恢复它?您是如何删除它的?从“您可以删除通知以将其从表中完全删除”中,通知不会实现软删除,当您对通知调用
    delete()
    时,它将从表中永久删除,您无法还原。我做了类似的操作。我在notifications表中创建了deleted_,在我删除通知的方法中,我并没有实际删除它,而是使用Carbon::now()更新列中的deleted_,然后在恢复时,我将列中的deleted_更新为null。我不必添加模型和软删除。这可以解决问题吗?我也做过类似的事情。我在notifications表中创建了deleted_,在我删除通知的方法中,我并没有实际删除它,而是使用Carbon::now()更新列中的deleted_,然后在恢复时,我将列中的deleted_更新为null。我不必添加模型和软删除。这样的解决方案行吗?行,但是您可以使用
    SoftDeletes
    来代替手动执行所有这些操作,这将减轻此麻烦。
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('notifications', function (Blueprint $table) {
            $table->softDeletes()->after('updated_at');
        });
    }
    
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('notifications', function (Blueprint $table) {
            $table->dropColumn('deleted_at');
        });
    }