Laravel 5访问会话用户

时间:2018-11-16 14:28:10

标签: laravel-5

当用户登录后,我发现该用户时,我会在屏幕上看到当前用户的所有属性。

$testing = $request->user();

dd($testing);

用户对象:

User {#596 ▼
  #table: "users"
  +primaryKey: "id"
  #fillable: array:3 [▶]
  #hidden: array:2 [▶]
  #connection: "mysql"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:9 [▶]
  #original: array:9 [▶]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #visible: []
  #guarded: array:1 [▶]
  #rememberTokenName: "remember_token"
}

如果要使用任何属性在控制器方法中运行用户检查,如何从控制器访问#attributes数组?

1 个答案:

答案 0 :(得分:2)

您可以通过以下方式访问属性:

magic method __get()

$request->user()->yourAttribute;

或直接调用getAttribute方法:

$request->user()->getAttribute('yourAttribute');