如何将软删除添加到通知表laravel

gcmastyq  于 2023-01-27  发布在  其他
关注(0)|答案(2)|浏览(146)

我正在使用laravel默认数据库通知,我想将 softdelete 添加到通知表中。
我已使用软删除创建迁移,该迁移已将***deleted_at**列添加到通知表。问题是我必须将“use SoftDeletes”添加到通知模型( 根据laravel文档 *),但找不到通知模型。

$table->softDeletes();

我尝试将'use SoftDeletes'添加到***HasDatabaseNotifications***特征,但它仍然删除该行。是否有其他方法将 softdelete 添加到通知表。TIA

wvt8vs2t

wvt8vs2t1#

在您的模型顶部开始类之前使用

use Illuminate\Database\Eloquent\SoftDeletes;

课后

class Notification extends Model
{
    use SoftDeletes;

     /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = ['deleted_at'];
}
ars1skjm

ars1skjm2#

这就是我的解决方法,希望对你和其他朋友有用
App\Classes\MyDatabaseNotification.php

namespace App\Classes;

use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\DatabaseNotification;

class MyDatabaseNotification extends DatabaseNotification
{
    use SoftDeletes;

    protected $dates = ['deleted_at'];
}

App\Classes\MyNotifiable.php

namespace App\Classes;

use Illuminate\Notifications\Notifiable;

trait MyNotifiable
{
    use Notifiable;

    /**
     * Get the entity's notifications.
     */
    public function notifications()
    {
        return $this->morphMany(MyDatabaseNotification::class, 'notifiable')
            ->orderBy('created_at', 'desc');
    }
}

App\User.php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Classes\MyNotifiable;
...

class User extends Authenticatable
{
    use MyNotifiable;
  ...

相关问题