How to add Softdeletes to Notifications Table laravel - laravel

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

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

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;
...

Related

Laravel Auditing package working only on User model and not working on other models

If I try to put on another model only new created record is auditing. Update, delete is not working.
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
use Illuminate\Database\Eloquent\Model;
use OwenIt\Auditing\Contracts\Auditable;
class Brands extends Model implements Auditable
{
use \OwenIt\Auditing\Auditable;
use Notifiable;
use HasRoles;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'brand_name', 'brand_status'
];
protected $auditInclude = [
'brand_name', 'brand_status'
];
}
I don't know why in User model it is tracking all events. But in another model, only the newly created record is tracking and not other things like deleting, updating it is not tracking.
Inside your config/audit.php , make sure that the timestamps setting is set to true. This allows the created_at, updated_at and deleted_at timestamps to be audited.
'timestamps' => true,
EDIT: Add created_at, updated_at and deleted_at inside your protected $auditInclude fuunction
protected $auditInclude = [
'brand_name', 'brand_status', 'created_at', 'updated_at', 'deleted_at'
];

laravel voyager soft delete issue

I'm trying to get soft deleting work on laravel voyager but everytime I delete a post it's also deleted in my database.
here is my post model :
<?php
namespace App;
use TCG\Voyager\Traits\Resizable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Category;
class Post extends Model
{
use Resizable;
use SoftDeletes;
protected $dates = ['deleted_at'];
public function category()
{
return $this->belongsTo(Category::class);
}
public function scopeIspublished($query)
{
return $query->where('status','published');
}
}
I added a column deleted_at to my posts table as well.
Thanks for helping

Error! Trait method restore has not been applied, because there are collisions with other trait methods on App\User- laravel softdelete

After using softdelete i got this error, how to fix softdelete with other trait..
class User extends Authenticatable
{
use HasApiTokens, Notifiable;
use EntrustUserTrait {
can as traitCan;
hasRole as traitHasRole;
}
use SoftDeletes;
protected $table = 'users';
finally i found my answer:
I decided to use softdelete restore instead.
use HasApiTokens, Notifiable;
use EntrustUserTrait {
can as traitCan;
hasRole as traitHasRole;
}
use SoftDeletes { SoftDeletes::restore insteadof EntrustUserTrait; }
or if you want to use both:
use SoftDeletes,EntrustUserTrait{
SoftDeletes::restore as soft_delete_restore;
EntrustUserTrait::restore as entrust_restore;
}
Add this code and it will work:
public function restore()
{
$this->restoreA();
$this->restoreB();
}

Laravel 5.2 soft delete does not work

I am have simple app with a table post and model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use SoftDeletes;
class Post extends Model
{
protected $table = 'post';
protected $dates = ['deleted_at'];
protected $softDelete = true;
}
I am trying to make example of soft delete and i am using route just for example route.php:
<?php
use App\Post;
use Illuminate\Database\Eloquent\SoftDeletes;
Route::get('/delete', function(){
$post = new Post();
Post::find(12)->delete();
});
I have a column "created_at" created with migration:
Schema::table('post', function (Blueprint $table) {
$table->softDeletes();
});
, but instead of adding time to this column, when I run the site it deletes the row with selected id. Where am I wrong ?
You need to use SoftDeletes trait inside your model like so:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Post extends Model
{
use SoftDeletes;
protected $table = 'post';
protected $dates = ['deleted_at'];
}
Now you, are not applying trait, so obviously it doesn't work.
In addition you have unnecessary piece of code in your routes file. It should look like this:
<?php
use App\Post;
Route::get('/delete', function(){
Post::find(12)->delete();
});

Laravel Eloquent - Soft deleting is not working

I have added $table->softDeletes() to my migration file and have added the protected $softDelete = true; to my model, and I do have the deleted_at in my table. But whenever I call delete(), the entire row is deleted. Here is how I am calling my delete:
Schedule::whereId($id)->whereClientId($client_id)->delete();
What am I doing wrong?
In case somebody bumps into this question. You can still use SoftDeletes in Laravel 5. Just use the softdeletes trait.
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes; //<--- use the softdelete traits
class YourModel extends Model
{
use SoftDeletes; //<--- use the softdelete traits
protected $dates = ['deleted_at']; //<--- new field to be added in your table
protected $fillable = [
];
}
You can view laravel docs or This great tutorial on how to softdelete
If you're overriding \Illuminate\Database\Eloquent\Model::boot for whatever reason, ensure you call parent::boot in the method so traits (like SoftDeletes) can be booted.

Resources