Add entity_id and entity_type columns to notifications table and change notification events from present to past listeners
MODIFY
app/Earning.php
app/Earning.php
namespace App;
-use App\Events\TransactionCreating;
-use App\Events\TransactionDeleting;
+use App\Events\TransactionCreated;
+use App\Events\TransactionDeleted;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
@@ -15,8 +15,8 @@ class Earning extends Model {
protected $fillable = ['happened_on', 'description', 'amount'];
protected $dispatchesEvents = [
- 'creating' => TransactionCreating::class,
- 'deleting' => TransactionDeleting::class
+ 'created' => TransactionCreated::class,
+ 'deleted' => TransactionDeleted::class
];
// Accessors

RENAME
app/Events/TagCreating.php
app/Events/TagCreated.php
@@ -12,13 +12,15 @@ use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
-class TagCreating {
+class TagCreated {
use Dispatchable, InteractsWithSockets, SerializesModels;
public function __construct(Tag $tag) {
Notification::create([
'space_id' => $tag->space_id,
'user_id' => \Auth::user()->id,
+ 'entity_id' => $tag->id,
+ 'entity_type' => 'tag',
'action' => 'tag.created'
]);
}

RENAME
app/Events/TagDeleting.php
app/Events/TagDeleted.php
@@ -12,13 +12,14 @@ use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
-class TagDeleting {
+class TagDeleted {
use Dispatchable, InteractsWithSockets, SerializesModels;
public function __construct(Tag $tag) {
Notification::create([
'space_id' => $tag->space_id,
- 'user_id' => \Auth::user()->id,
+ 'entity_id' => $tag->id,
+ 'entity_type' => 'tag',
'action' => 'tag.deleted'
]);
}

RENAME
app/Events/TransactionCreating.php
app/Events/TransactionCreated.php
namespace App\Events;
+use App\Earning;
use App\Notification;
+use App\Spending;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
@@ -11,13 +13,23 @@ use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
-class TransactionCreating {
+class TransactionCreated {
use Dispatchable, InteractsWithSockets, SerializesModels;
public function __construct($transaction) {
+ if ($transaction instanceof Earning) {
+ $entityType = 'earning';
+ }
+
+ if ($transaction instanceof Spending) {
+ $entityType = 'spending';
+ }
+
Notification::create([
'space_id' => $transaction->space_id,
'user_id' => \Auth::user()->id,
+ 'entity_id' => $transaction->id,
+ 'entity_type' => $entityType,
'action' => 'transaction.created'
]);
}

RENAME
app/Events/TransactionDeleting.php
app/Events/TransactionDeleted.php
namespace App\Events;
+use App\Earning;
use App\Notification;
+use App\Spending;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
@@ -11,13 +13,23 @@ use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
-class TransactionDeleting {
+class TransactionDeleted {
use Dispatchable, InteractsWithSockets, SerializesModels;
public function __construct($transaction) {
+ if ($transaction instanceof Earning) {
+ $entityType = 'earning';
+ }
+
+ if ($transaction instanceof Spending) {
+ $entityType = 'spending';
+ }
+
Notification::create([
'space_id' => $transaction->space_id,
'user_id' => \Auth::user()->id,
+ 'entity_id' => $transaction->id,
+ 'entity_type' => $entityType,
'action' => 'transaction.deleted'
]);
}

MODIFY
app/Notification.php
app/Notification.php
@@ -5,7 +5,7 @@ namespace App;
use Illuminate\Database\Eloquent\Model;
class Notification extends Model {
- protected $fillable = ['space_id', 'user_id', 'action'];
+ protected $fillable = ['space_id', 'user_id', 'entity_id', 'entity_type', 'action'];
public function space() {
return $this->belongsTo(Space::class);

MODIFY
app/Spending.php
app/Spending.php
namespace App;
-use App\Events\TransactionCreating;
-use App\Events\TransactionDeleting;
+use App\Events\TransactionCreated;
+use App\Events\TransactionDeleted;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
@@ -22,8 +22,8 @@ class Spending extends Model {
];
protected $dispatchesEvents = [
- 'creating' => TransactionCreating::class,
- 'deleting' => TransactionDeleting::class
+ 'created' => TransactionCreated::class,
+ 'deleted' => TransactionDeleted::class
];
// Accessors

MODIFY
app/Tag.php
app/Tag.php
namespace App;
-use App\Events\TagCreating;
-use App\Events\TagDeleting;
+use App\Events\TagCreated;
+use App\Events\TagDeleted;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
@@ -15,8 +15,8 @@ class Tag extends Model {
protected $fillable = ['space_id', 'name'];
protected $dispatchesEvents = [
- 'creating' => TagCreating::class,
- 'deleting' => TagDeleting::class
+ 'created' => TagCreated::class,
+ 'deleted' => TagDeleted::class
];
public function spendings() {

MODIFY
database/migrations/2018_12_17_174821_create_notifications_table.php
database/migrations/2018_12_17_174821_create_notifications_table.php
@@ -10,6 +10,8 @@ class CreateNotificationsTable extends Migration {
$table->increments('id');
$table->unsignedInteger('space_id');
$table->unsignedInteger('user_id')->nullable();
+ $table->unsignedInteger('entity_id');
+ $table->string('entity_type');
$table->string('action');
$table->timestamps();

MODIFY
resources/views/notifications/index.blade.php
resources/views/notifications/index.blade.php
<img class="avatar" src="{{ $notification->user->avatar }}" />
@endif
</div>
- <div class="row__column row__column--middle">{{ __('notifications.' . $notification->action) }}</div>
+ <div class="row__column row__column--middle">{{ __('notifications.' . $notification->action) }} <a href="/{{ $notification->entity_type }}s/{{ $notification->entity_id }}">#{{ $notification->entity_id }}</a></div>
<div class="row__column row__column--middle row__column--compact">{{ $notification->created_at->diffForHumans() }}</div>
</div>
@endforeach

Add entity_id and entity_type columns to notifications table and change notification events from present to past listeners