3

I am using laravel default database notifications, I want to add softdelete to notifications table.

I have created a migration with softdelete which has added deleted_at column to the notifications table. The problem is I have to add 'use SoftDeletes' to notifications model(according to laravel docs) but cannot find the notifications model.

 $table->softDeletes();

I tried adding 'use SoftDeletes' to HasDatabaseNotifications trait but it still deletes the row. Is there another way to add softdelete to notifications table. TIA

TALHA001
  • 151
  • 1
  • 13

2 Answers2

1

In your model at top before start class use

use Illuminate\Database\Eloquent\SoftDeletes;

After class

class Notification extends Model
{
    use SoftDeletes;

     /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = ['deleted_at'];
}
Dilip Hirapara
  • 14,810
  • 3
  • 27
  • 49
  • Thanks @Dilip Hirapara, but I cannot find the Notification Model beacause I am using default laravel notifications – TALHA001 Aug 20 '19 at 10:26
  • Have you check https://laracasts.com/discuss/channels/laravel/how-use-custom-database-notification-model or https://stackoverflow.com/questions/43646085/laravel-5-4-5-3-customize-or-extend-notifications-database-model – Dilip Hirapara Aug 20 '19 at 10:30
  • both question and answer from Kamal Khan. – Dilip Hirapara Aug 20 '19 at 10:38
  • I looked at these threads but they are different, my problem is that I cannot find the notification model where I have to use softdelete, may be there's no option of soft deleting in laravel notifications – TALHA001 Aug 20 '19 at 10:45
  • I think you need to override your notification model as @Kamal Khan did. check your `Illuminate\Notifications\Notifiable` if Notifiable class exists then you may override it, and after you can use it as model like I explain soft delete. First follow the @Kamal Khan answer check it possible. I really don't know about notification can be softdelete or not but he followed to changes in model so might be possible. – Dilip Hirapara Aug 20 '19 at 10:51
0

This is how I solved it, I hope it will be useful for you and other friends

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;
  ...
Sh4msi
  • 1,231
  • 13
  • 15