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