Skip to content

Commit

Permalink
添加消息通知
Browse files Browse the repository at this point in the history
  • Loading branch information
broqiang committed Mar 9, 2018
1 parent 8c459d7 commit c91dfe2
Show file tree
Hide file tree
Showing 12 changed files with 251 additions and 7 deletions.
24 changes: 24 additions & 0 deletions app/Http/Controllers/NotificationsController.php
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'));
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/PostsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function unfollow(Post $post)
public function comment(Post $post, Request $request)
{
$this->validate($request, [
'content' => 'required|string|min:5|max:128',
'content' => 'required|string|min:5|max:256',
]);

$comment = $request->all();
Expand Down
22 changes: 21 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,23 @@
use App\Models\Post;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Auth;

class User extends Authenticatable
{
use Notifiable;
use Notifiable {
notify as protected laravelNotify;
}

public function notify($instance)
{
// 如果要通知的人是当前用户,就不必通知了!
if ($this->id == Auth::id()) {
return;
}
$this->increment('notification_count');
$this->laravelNotify($instance);
}

/**
* The attributes that are mass assignable.
Expand Down Expand Up @@ -62,4 +75,11 @@ public function comments()
{
return $this->hasMany(Comment::class)->orderBy('comments.updated_at', 'desc');
}

public function markAsRead()
{
$this->notification_count = 0;
$this->save();
$this->unreadNotifications->markAsRead();
}
}
81 changes: 81 additions & 0 deletions app/Notifications/PostCommented.php
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 [
//
];
}
}
4 changes: 4 additions & 0 deletions app/Observers/CommentObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace App\Observers;

use App\Models\Comment;
use App\Notifications\PostCommented;
use Illuminate\Support\Carbon;

class CommentObserver
Expand All @@ -15,6 +16,9 @@ public function created(Comment $comment)

// 更新文章的最后更新时间
$post->update(['updated_at' => Carbon::now()]);

// 发送通知
$post->user->notify(new PostCommented($comment));
}

public function deleted(Comment $comment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AddNewFieldToUsersTable extends Migration
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('avatar')->default('');
$table->string('avatar')->default('http://image.broqiang.com/avatar_default_php.png');
$table->string('realname',64)->default('');
$table->string('introduction')->default('');
$table->string('tel',64)->default('');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function up()
$table->increments('id');
$table->integer('post_id');
$table->integer('user_id');
$table->string('content',512);
$table->string('content',1024);
$table->timestamps();
});
}
Expand Down
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');
}
}
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');
});
}
}
8 changes: 5 additions & 3 deletions resources/views/layouts/_header_right.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
@else

<li class="mr-3">
<a class="nav-link secondary" href="#">
<i class="fa fa-bell"></i>
<span class="badge badge-light">0</span>
<a class="nav-link" href="{{ route('notifications.index') }}">
<span class="badge badge-light {{ Auth::user()->notification_count > 0 ? 'text-danger' : 'text-muted' }}" title="消息通知提醒">
<i class="fa fa-bell"></i>
{{ Auth::user()->notification_count }}
</span>
</a>
</li>

Expand Down
42 changes: 42 additions & 0 deletions resources/views/notifications/index.blade.php
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
4 changes: 4 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@

/** 技能分类 */
Route::resource('skills', 'SkillsController', ['only' => ['show']]);

/** 消息通知 */
Route::resource('notifications', 'NotificationsController', ['only' => ['index']]);

0 comments on commit c91dfe2

Please sign in to comment.