laravel版本:5.0
我目前在laravel管理登录中间件。
我使用laravel默认身份验证。在此状态下,登录成功。
如我所知,我可以通过执行Auth::User()->id;
获得用户id,但问题是:我找不到方法来检索表名;
下面是如果我执行dd(Auth::User());
如你所见,包含了信息#table:"admins"
Admin {#177 ▼
#table: "admins"
#fillable: array:3 [▶]
#hidden: array:2 [▶]
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:7 [▶]
#original: array:7 [▶]
#relations: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#dates: []
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
}
这是我的中间件,我想检查表名是否为admin:
<?php namespace App'Http'Middleware;
use Closure;
use Auth;
class AdminMiddleware extends Model{
/**
* Handle an incoming request.
*
* @param 'Illuminate'Http'Request $request
* @param 'Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if(Auth::User()->getTable() == 'admins') //this doesnt work
{
return $next($request);
}
else
{
return response('Unauthorized.', 401);
}
}
}
我试图寻找它从laravel文档和搜索在stackoverflow,似乎我什么也得不到。
Q:我需要从Auth::调用什么方法来获取表名?
如果有更好的做法,不要犹豫,回答
如果您的Admin class
正在扩展Eloquent'Model
, Admin::getTable()
或$adminInstance->getTable()
应该工作。
不要为admin和users创建两个单独的表,而是将他们的凭据放在同一个表中,并使用第二个表,即roles,其中包含用户角色的详细信息。如果你想要相同的表结构和代码请告诉我,我会分享的