Laravel-通知控制器可以';t使用方法user()
我正在尝试将通知系统添加到我的Laravel项目中。我通过观看此视频了解系统: 已创建通知表、模型et控制器。此外,我还使用Vue.JS和Pusher创建了视图。很好 但是,在通知控制器中,当我尝试使用user Auth::user()方法时,它返回null。我在某个地方读到,这是因为当控制器运行时,中间件“auth”还没有加载 这是我的NotificationsController文件:Laravel-通知控制器可以';t使用方法user(),laravel,authentication,controller,notifications,Laravel,Authentication,Controller,Notifications,我正在尝试将通知系统添加到我的Laravel项目中。我通过观看此视频了解系统: 已创建通知表、模型et控制器。此外,我还使用Vue.JS和Pusher创建了视图。很好 但是,在通知控制器中,当我尝试使用user Auth::user()方法时,它返回null。我在某个地方读到,这是因为当控制器运行时,中间件“auth”还没有加载 这是我的NotificationsController文件: <?php namespace App\Http\Controllers; use Ap
<?php
namespace App\Http\Controllers;
use App\Notification;
use Illuminate\Http\Request;
use App\Idea;
use App\User;
use Illuminate\Support\Facades\Auth;
class NotificationsController extends Controller
{
public function __construct()
{
}
function get(){
$notification = Auth::user()->unreadNotifications()->get();
return $notification;
}
function read(Request $request){
Auth::user()->unreadNotifications()->find($request->id)->markAsRead();
}
}
答案不是关于Auth::user(可以访问)。我刚刚在我的模型中定义了应报告的类型。它是App\Idea,应该是App\UserAuth
方法在构造函数中不可用(因为会话中间件尚未运行),但在其他方法中可用。因此,在您的情况下,您只是没有登录。可以肯定,我正在登录(我在右角看到用户名),但什么都没有,auth return null如何设置路由?这是此控制器的路由://notif路由的路由::post('/notification/get','NotificationsController@get')->中间件('auth');路由::post('/notification/read','NotificationsController@read')->中间件('auth')代码>