forked from broqiang/www.broqiang.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
251 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Auth; | ||
|
||
class NotificationsController extends Controller | ||
{ | ||
public function __construct() | ||
{ | ||
$this->middleware('auth'); | ||
} | ||
|
||
public function index() | ||
{ | ||
// 获取登录用户的所有通知 | ||
$notifications = Auth::user()->notifications()->paginate(20); | ||
|
||
Auth::user()->markAsRead(); | ||
|
||
return view('notifications.index', compact('notifications')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace App\Notifications; | ||
|
||
use App\Models\Comment; | ||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Notifications\Messages\MailMessage; | ||
use Illuminate\Notifications\Notification; | ||
|
||
class PostCommented extends Notification | ||
{ | ||
|
||
use Queueable; | ||
|
||
protected $comment; | ||
|
||
/** | ||
* Create a new notification instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct(Comment $comment) | ||
{ | ||
// | ||
$this->comment = $comment; | ||
} | ||
|
||
/** | ||
* Get the notification's delivery channels. | ||
* | ||
* @param mixed $notifiable | ||
* @return array | ||
*/ | ||
public function via($notifiable) | ||
{ | ||
return ['database']; | ||
} | ||
|
||
public function toDatabase($notifiable) | ||
{ | ||
$post = $this->comment->post; | ||
$user = $this->comment->user; | ||
|
||
return [ | ||
'post_id' => $post->id, | ||
'post_title' => $post->title, | ||
'user_id' => $user->id, | ||
'user_name' => $user->name, | ||
'user_avatar' => $user->avatar, | ||
'comment_id' => $this->comment->id, | ||
'comment_content' => $this->comment->content, | ||
]; | ||
} | ||
|
||
/** | ||
* 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 [ | ||
// | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
database/migrations/2018_03_10_003648_create_notifications_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class CreateNotificationsTable 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'); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
database/migrations/2018_03_10_003730_add_notification_count_to_users_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class AddNotificationCountToUsersTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::table('users', function (Blueprint $table) { | ||
$table->unsignedInteger('notification_count')->default(0)->comment('未读消息数量'); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::table('users', function (Blueprint $table) { | ||
$table->dropColumn('notification_count'); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
@extends('layouts.app') | ||
|
||
@section('content') | ||
<div class="container"> | ||
<div class="card"> | ||
<div class="card-header bg-white"> | ||
<h3 class="text-muted text-center p-3"> | ||
<i class="fa fa-bell-o text-success"></i> | ||
我的通知 | ||
</h3> | ||
</div> | ||
<ul class="list-group"> | ||
@foreach($notifications as $notification) | ||
<li class="list-group-item text-muted"> | ||
<span class="pull-left"> | ||
<a class="text-info" href="{{ route('users.show', $notification->data['user_id']) }}"> | ||
<img src="{{ $notification->data['user_avatar'] }}" alt="" width="22"> | ||
{{ $notification->data['user_name'] }} | ||
</a> | ||
对 | ||
<a class="text-info" href="{{ route('posts.show', $notification->data['post_id']) }}"> | ||
{{ $notification->data['post_title'] }} | ||
</a> | ||
评论了: | ||
</span> | ||
<small class="pull-right"> | ||
<i class="fa fa-clock-o"></i> {{ $notification->created_at->diffForHumans() }} | ||
</small> | ||
|
||
<div class="clearfix"></div> | ||
|
||
<div class="card my-3"> | ||
<div class="card-body"> | ||
{{ $notification->data['comment_content'] }} | ||
</div> | ||
</div> | ||
</li> | ||
@endforeach | ||
</ul> | ||
</div> | ||
</div> | ||
@endsection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters