duanjihe5180 2019-06-24 06:04 采纳率: 100%
浏览 72

VerifyCsrfToken,Auth&Trying获取非对象的属性

I'm going back to an old project, and the homepage need to be access when logged in, if not, you get redirected to /login. For that I'm using the Auth middleware, but when doing so, I get the following error (see bottom of post with the error and the full error on imgur).

I checked my middleware but don't actually have custom one, I also checked my code to see if I weren't using a weird middleware group or anything, and I can't seem to find an error for now.

In my HomeController.php, if I remove the usage of the Auth middleware, the script continue (but I have another error further along, about Roles & Permissions, which is normal)

    public function __construct()
    {
        $this->middleware('auth');
    }

Normally, I would expect to be redirected, but instead, I get this error : Trying to get property of non-object

Full error : Refer this

I shall mention that I have this error on EVERY page that need authentification, and not on the one that doesn't, it's not limited to my HomeController

Thanks !

EDIT : Here is the full controler where I use the middleware (even if I have the issue elsewhere too)

<?php

namespace App\Http\Controllers;

use App\Sinistre;
use App\Appointment;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $sinistres = null;
        if (Auth::user()->hasRole('admin')) {
            $sinistres = Sinistre::all();
        } else {
            $sinistres = Auth::user()->sinistres;
            foreach(Auth::user()->societe AS $societe){
                $sinistres = $sinistres->merge($societe->sinistre);
            }
        }

        $sinistres = $sinistres->unique();

        $appointments = Appointment::orderBy('date_start', 'desc')->with(['sinistre', 'sinistre.user'])->take(8)->get()->reverse();
        return view('pages.home', [
            'sinistres' => $sinistres,
            'appointments' => $appointments,
        ]);
    }
}
  • 写回答

1条回答 默认 最新

  • dpdbu24262 2019-06-24 06:47
    关注

    it might mean that you're not logged in try to add this one in the beginning of your index function in controller

        if(Auth::user()){
         //Your Code
        }else{
         return redirect('/');
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗