forked from summerblue/larabbs
-
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
1 parent
aaec05b
commit 7ec12fc
Showing
5 changed files
with
128 additions
and
1 deletion.
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
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,46 @@ | ||
<?php | ||
|
||
namespace App\Notifications; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Notifications\Notification; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Notifications\Messages\MailMessage; | ||
use App\Models\Reply; | ||
|
||
class TopicReplied extends Notification | ||
{ | ||
use Queueable; | ||
|
||
public $reply; | ||
|
||
public function __construct(Reply $reply) | ||
{ | ||
// 注入回复实体,方便 toDatabase 方法中的使用 | ||
$this->reply = $reply; | ||
} | ||
|
||
public function via($notifiable) | ||
{ | ||
// 开启通知的频道 | ||
return ['database']; | ||
} | ||
|
||
public function toDatabase($notifiable) | ||
{ | ||
$topic = $this->reply->topic; | ||
$link = $topic->link(['#reply' . $this->reply->id]); | ||
|
||
// 存入数据库里的数据 | ||
return [ | ||
'reply_id' => $this->reply->id, | ||
'reply_content' => $this->reply->content, | ||
'user_id' => $this->reply->user->id, | ||
'user_name' => $this->reply->user->name, | ||
'user_avatar' => $this->reply->user->avatar, | ||
'topic_link' => $link, | ||
'topic_id' => $topic->id, | ||
'topic_title' => $topic->title, | ||
]; | ||
} | ||
} |
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/2019_09_20_095702_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'); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
database/migrations/2019_09_20_095955_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,22 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class AddNotificationCountToUsersTable extends Migration | ||
{ | ||
public function up() | ||
{ | ||
Schema::table('users', function (Blueprint $table) { | ||
$table->integer('notification_count')->unsigned()->default(0); | ||
}); | ||
} | ||
|
||
public function down() | ||
{ | ||
Schema::table('users', function (Blueprint $table) { | ||
$table->dropColumn('notification_count'); | ||
}); | ||
} | ||
} |