|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PHPHub\Http\ApiControllers; |
|
4
|
|
|
|
|
5
|
|
|
use Auth; |
|
6
|
|
|
use PHPHub\Repositories\NotificationRepositoryInterface; |
|
7
|
|
|
use PHPHub\Repositories\UserRepositoryInterface; |
|
8
|
|
|
use PHPHub\Transformers\NotificationTransformer; |
|
9
|
|
|
|
|
10
|
|
|
class NotificationController extends Controller |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var NotificationRepositoryInterface |
|
14
|
|
|
*/ |
|
15
|
|
|
private $notifications; |
|
16
|
|
|
/** |
|
17
|
|
|
* @var UserRepositoryInterface |
|
18
|
|
|
*/ |
|
19
|
|
|
private $users; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* NotificationController constructor. |
|
23
|
|
|
* |
|
24
|
|
|
* @param NotificationRepositoryInterface $repository |
|
25
|
|
|
* @param UserRepositoryInterface $user_repository |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct(NotificationRepositoryInterface $repository, UserRepositoryInterface $user_repository) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->notifications = $repository; |
|
30
|
|
|
$this->users = $user_repository; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* 获取当前用户的通知消息. |
|
35
|
|
|
* |
|
36
|
|
|
* @return \Illuminate\Http\Response |
|
37
|
|
|
*/ |
|
38
|
|
|
public function index() |
|
39
|
|
|
{ |
|
40
|
|
|
$this->notifications->addAvailableInclude('from_user', ['name', 'avatar']); |
|
41
|
|
|
$this->notifications->addAvailableInclude('reply', ['created_at']); |
|
42
|
|
|
$this->notifications->addAvailableInclude('topic', ['title']); |
|
43
|
|
|
|
|
44
|
|
|
$data = $this->notifications |
|
45
|
|
|
->userRecent(Auth::id()) |
|
46
|
|
|
->autoWith() |
|
47
|
|
|
->autoWithRootColumns(['id', 'type', 'body', 'topic_id', 'reply_id', 'created_at']) |
|
48
|
|
|
->paginate(per_page()); |
|
49
|
|
|
|
|
50
|
|
|
$this->users->setUnreadMessagesCount(Auth::id(), 0); |
|
51
|
|
|
|
|
52
|
|
|
return $this->response()->paginator($data, new NotificationTransformer()); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* 获取当前用户的未读消息数. |
|
57
|
|
|
* |
|
58
|
|
|
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response |
|
59
|
|
|
*/ |
|
60
|
|
|
public function unreadMessagesCount() |
|
61
|
|
|
{ |
|
62
|
|
|
$count = $this->users->getUnreadMessagesCount(); |
|
63
|
|
|
|
|
64
|
|
|
return response(compact('count')); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: