Reputation: 1950
I have some user data I need to share with all views, a list of 'types' owned by the user. I have setup a view::share to accomplish this, and placed it inside my routes.php inside an 'auth' filtered group like so:
routes.php
/* Authenticated group */
Route::group( array('before' => 'auth'), function() {
// Bring list of user's Types into views for sidebar menu generation
View::share( 'types', User::find( Auth::user()->id )->types );
/* Some user routes */
});
This works properly when the user is logged in, however when they are logged out it throws Trying to get property of non-object
error. Am I misunderstanding how route filtering works? I'm guessing this View::share
is still being processed even though it is inside the 'auth' filtered route group.
What is the proper way to do this? Should I create a Route::filter
to share the data and then apply it to the route group?
Upvotes: 0
Views: 283
Reputation: 5874
You might be able to just share the variable in your filters instead.
Assuming you use Laravel's default auth filters (in filters.php
):
Route::filter('auth', function()
{
if (Auth::guest()) {
View::share('types', 'not_logged_in');
return Redirect::guest('login');
}
else {
View::share('types', User::find( Auth::user()->id )->types);
// If you are not returning anything, this filter passes
}
});
Upvotes: 1
Reputation: 13354
It's because Auth::user()
returns non-object when not logged in. Need to account for this.
if ( Auth::check() ){
View::share( 'types', User::find( Auth::user()->id )->types );
} else {
View::share( 'types', $some_other_types_here );
}
Upvotes: 1