尝试在uselogin.php

时间:2018-11-09 17:48:18

标签: php laravel

您好,我使用larave,但这是代码和行if(is_null($StudentId)) $StudentId = Auth::user()->students_ID;导致尝试获取非对象错误的属性

namespace App\Models;


use App\Core\Actions;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;

class UserLog extends Model {
    protected $fillable = ['student_id','action','time'];
    public $timestamps = false;

    public static function Log($Action,$StudentId = null) {
       if(is_null($StudentId)) $StudentId = Auth::user()->students_ID;

        if(self::CheckLastMinute($Action,$StudentId)) return;
        self::create([
            'student_id' => $StudentId,
            'action' => $Action,
            'time' => date("Y-m-d H:i:s")
        ]);
    }

    public static function CheckLastMinute($Action,$StudentId = null) {
        if(is_null($StudentId)) $StudentId = Auth::user()->students_ID;
        return self::where('action',$Action)
            ->where('student_id',$StudentId)
            ->where('time','>=',Carbon::now()->subMinutes(1)->toDateTimeString())
            ->count();
    }
}

非常遗憾,丢失的零件不知道即时通讯的速度真的很差

1 个答案:

答案 0 :(得分:0)

假设您拥有经过身份验证的用户实例是一个坏主意。

在cli上下文或访客浏览您的网站时,如果您没有经过身份验证的用户,则Auth::user()将返回null。因此,您实际上是在这些情况下调用null->students_ID,这是导致错误的原因。