如何在Laravel 4中更改Auth :: user()的值 [英] How to change the value of Auth::user() in Laravel 4
本文介绍了如何在Laravel 4中更改Auth :: user()的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
任何更改Auth::user()
返回的用户实例的方法,我想要的是渴望加载与它的某些关系,因此我不必每次都保持键入:
Any way to change the user instance returned by Auth::user()
, what I want is to eager load some relations with it, so i don't have to keep typing it every time:
来自
Auth::user();
到
Auth::user()->with('company')->first();
,每次我请求Auth::user()
时,我都会得到返回的Auth::user()->with('company')->first()
.
and every time I request the Auth::user()
I get the Auth::user()->with('company')->first()
returned.
推荐答案
一种解决方法是编辑before
过滤器(app/filters.php
).
One way of doing it is to edit your before
filter (app/filters.php
).
App::before(function($request)
{
if (Auth::check())
{
Auth::setUser(Auth::user()->with('company')->first());
}
});
这样,您仍然可以在需要的地方使用Auth::user()
.
That way you can still use Auth::user()
wherever you need.
这篇关于如何在Laravel 4中更改Auth :: user()的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文