How to add Softdeletes to Notifications Table laravel

2k Views Asked by At

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

3

There are 3 best solutions below

5
Dilip Hirapara On

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'];
}
0
Sh4msi On

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;
  ...
0
Jan F On

This is how I solved the issue:

  1. Create a migration to add a deleted_at column to notifications table:

    /**
    * Run the migrations.
    *
    * @return void
    */
    public function up()
    {
        Schema::table('notifications', function (Blueprint $table) {
            $table->softDeletes();
        });
    }
    
    /**
    * Reverse the migrations.
    *
    * @return void
    */
    public function down()
    {
        Schema::table('notifications', function (Blueprint $table) {
            $table->dropColumn(['deleted_at']);
        });
    }
    
  2. Create a new model extending the DatabaseNotification model class and add the SoftDeletes trait. The DatabaseNotification class extends the model class, so it's basically just a model:

    use Illuminate\Database\Eloquent\SoftDeletes;
    use Illuminate\Notifications\DatabaseNotification;
    
    class Notification extends DatabaseNotification
    {
        use SoftDeletes;
        ...
    
  3. Optionally create a NotificatonController class and implement the delete function. You have to use your new Notification class as a param. Using the original DatabaseNotification class will not work for soft deletion:

    class NotificationsController extends Controller
    {
        ...
    
        /**
         * @param  Notification  $notification
         * @return JsonResponse
         */
         public function delete(Notification $notification)
         {
             $notification->delete();
    
             return new JsonResponse(null, 204);
         }
    
         ...
    }