Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions app/Http/Controllers/PaymentsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Notification;

use App\Notifications\PaymentRecieved;


class PaymentsController extends Controller
{

public function create()
{
}

public function store()
{
request()->user()->notify(new PaymentRecieved(10));
}
}
18 changes: 18 additions & 0 deletions app/Http/Controllers/UserNotificationsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Notifications\DatabaseNotification;

class UserNotificationsController extends Controller
{

public function index()
{
$notifications = auth()->user()->unreadnotifications;
$notifications->each(fn ($notification) => $notification->markAsRead());

return view('notifications.show', ['notifications' => $notifications]);
}
}
62 changes: 62 additions & 0 deletions app/Notifications/PaymentRecieved.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class PaymentRecieved extends Notification
{
use Queueable;
protected $amount;

/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($amount)
{
$this->amount = $amount;
}

/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail', 'database'];
}

/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}

/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
'amount' => $this->amount
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('notifications', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('type');
$table->morphs('notifiable');
$table->text('data');
$table->timestamp('read_at')->nullable();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('notifications');
}
};
17 changes: 17 additions & 0 deletions resources/views/notifications/show.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@extends('layouts.guest')

@section('content')
<div class="container">
<ul>
@forelse (notifications as notification)
<li>
@if ($notification->type === 'App\Notifications\PaymentRecieved')
we have recieved a payment of {{ $notification->data['amount'] }} from you
@endif
</li>
@empty
<li>you have no unread notifications at this time </li>
@endforelse
</ul>
</div>
@endsection
1 change: 1 addition & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@
Route::resource('products', ProductController::class);
Route::resource('media', MediaController::class)->only('destroy');
});
Route::get('notifications', [\App\Http\Controllers\UserNotificationsController::class, 'show'])->middleware('auth');