dynamitem
dynamitem

Reputation: 1669

Laravel 4 Public Functions

I've got a question. I made a function that says if 'type' (a column in my database) is equal to five, it'll display buttons that others can't view. The problem is when I log out or log into a user that doesn't have type equals to five, it displays an error. How could I do this in a function? I tried various things, but it always displays errors. Here's my method...

<?php

public function get_dash()
{
    $roles = Auth::user()->type;

    if ($roles == '5') {
        return View::make('admin.dash')->with('roles', $roles);
    } else {
        return Redirect::to('news/index')
            ->with('danger', 'You either have insufficient permissions 
                   to access this page or your user credentials 
                   are not refreshed.');
    }
}

I basically want it so that if no type equals to five in an account, or when I log out it'll load normally...

return View::make('news/index');

Upvotes: 0

Views: 393

Answers (2)

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87739

When a user is not authenticated, you have no access to ´Auth::user()´, so you need something like this:

public function get_index()
{
    if( ! Auth::guest() ) 
    {
        $roles = Auth::user()->type;

        return View::make('aac.index')
                ->with('newss', News::all())
                ->with('roles', $roles);
    }
    else 
    {
        return Redirect::to('login');
    }
}

Upvotes: 0

user2647338
user2647338

Reputation: 61

Before trying to access the User object, check to make sure that the user is in fact authenticated by using Auth::check() as specified in the manual.

if (Auth::check())
{
    // The user is logged in...
}

Upvotes: 2

Related Questions