【发布时间】:2021-06-03 09:07:40
【问题描述】:
我创建了一个通知,用于获取所有最新通知,并在单击它们时将它们标记为已读。
UserNotifications.vue:
<template>
<li class="nav-item dropdown" v-if="notifications.length">
<a class="nav-link" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre>
<span class="fas fa-bell"></span>
</a>
<ul class="dropdown-menu">
<li v-for="notification in notifications" :key="notification.id">
<a :href="notification.data.link"
v-text="notification.data.message"
@click="markAsRead(notification)"
></a>
</li>
</ul>
</li>
</template>
<script>
export default {
data() {
return {notifications: false}
},
created() {
axios.get('/profiles/' + window.App.user.name + '/notifications')
.then(response => this.notifications = response.data);
},
methods: {
markAsRead(notification) {
axios.delete('/profiles/' + window.App.user.name + '/notifications/' + notification.id)
}
}
}
</script>
用户通知控制器:
class UserNotificationsController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index(){
return auth()->user()->unreadNotifications;
}
public function destroy($user,$notificationId){
auth()->user()->notifications()->findOrFail($notificationId)->markAsRead();
}
}
web.php:
//Get notification for user
Route::get('/profiles/{user}/notifications','UserNotificationsController@index')->name('notification.index');
//Delete notification
Route::delete('/profiles/{user}/notifcations/{notification}','UserNotificationsController@destroy')->name('notifcation.destroy');
虽然获取未读通知有效,但当我单击通知时,它返回了未找到响应错误 404。通知表具有正确的 notificationId,但无法将其标记为已读。我不明白为什么它不能工作。
在通知表中,点击未读通知后,read_at 列为NULL。有谁知道为什么?
【问题讨论】:
-
请求的 URL 最后是否有正确的通知 ID?
-
@Rwd 是的,上面写着
DELETE http://forum.test/profiles/Nicholas/notifications/f6d095e2-e342-42a8-a294-0e3187c11dcb 404 (Not found)。我检查了我的通知表 ID,它与 URL 上的哈希值匹配。 -
您是否能够通过用户手动使用修补程序(或单独的路线)检索通知,例如
$user->notifications()->findOrFail('f6d095e2-e342-42a8-a294-0e3187c11dcb')? -
我进一步检查了失败的请求,它说:“Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException”
-
完全不用担心。我们都去过!很高兴我能帮忙:)
标签: laravel