php - 试图在 uselogin.php 中获取非对象的属性
问题描述
嗨,我正在使用 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();
}
}
非常抱歉,丢失的部分不知道我今天的连接速度很慢
解决方案
假设您有一个经过身份验证的用户实例是一个坏主意。
当您没有经过身份验证的用户时,无论是在 cli 上下文中还是来宾浏览您的站点,Auth::user()
都将返回 null。因此,您实际上是null->students_ID
在这些上下文中调用,这就是错误的原因。
推荐阅读
- html - CSS 网格属性:创建响应式全屏“纵向”图像网格,保留 767 及以上屏幕宽度的图像纵横比
- c++ - 虽然运行了一段时间,但程序已经停止工作?
- python - 在 Python 诅咒中显示扩展的 Unicode 字符
- matlab - 正则化逻辑回归计算代价函数和梯度面临的问题
- android - Firebase Phone Auth 在发布版本中不起作用
- phaser-framework - 对 Phaser 3 物质体使用多个动画
- json - git log --pretty=format windows 奇怪的行为
- wpf - Prism - 订阅的事件不会触发
- git - 无法在 MAC MOJAVE 上卸载 GIT
- scheme - 具有当前继续调用的 Scheme 程序中的控制流