0

I am getting this error

"Call to a member function isATeamManager() on a non-object".


(RedirectIfNotAManager.php)

<?php

namespace App\Http\Middleware;
use Illuminate\Http\Request;
use Closure;


class RedirectIfNotAManager
{


    public function handle($request, Closure $next)
    {
        if(!$request->user()->isATeamManager())
        {

            return redirect('articles');

        }

        return $next($request);
    }
}

I have googled it and didn't get any solution,since i am new to laravel kindly help me to solve this problem .its in laravel 5.1 . I have tried other examples and still getting this error..

(This is the User.php Model code:)

<?php

namespace App;


class User extends Model implements AuthenticatableContract,
                                    AuthorizableContract,
                                    CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword;

     protected $table = 'users';


    protected $fillable = ['name', 'email', 'password'];


    protected $hidden = ['password', 'remember_token'];


    public function articles()

    {


        return $this->hasMany('App\Article');
    }

    public function isATeamManager()
    {

        return false;
    }
}
Djizeus
  • 4,161
  • 1
  • 24
  • 42
egom
  • 21
  • 5
  • 3
    https://stackoverflow.com/questions/54566/call-to-a-member-function-on-a-non-object?rq=1 https://stackoverflow.com/questions/254291/call-to-a-member-function-on-a-non-object?rq=1 https://stackoverflow.com/questions/1787561/call-to-a-member-function-on-a-non-object?rq=1 https://stackoverflow.com/questions/10287023/call-to-member-function-on-non-object?rq=1 ..... – Ghedipunk Sep 25 '15 at 16:45
  • thanks for you help!!! – egom Sep 25 '15 at 18:05

2 Answers2

1

That means that your request doesn't have a user stored on it. So no one is logged in or your session isn't working correctly. $request->user() is a function that runs to try to pull the current user, by default if someone is logged in it will return a user object or a null value I believe. So most likely you are getting a null value back. You could change your if statement to this:

if(!$request->user() || !$request->user()->isATeamManager()) {
Pitchinnate
  • 7,517
  • 1
  • 20
  • 37
  • Thanks!! you are Right!! i got the solution...it's only work when user is logged in..i din't not log in was trying that.. if ($request->user()) { if (!$request->user()->isATeamManager()) { } } – egom Sep 25 '15 at 18:08
0

i got the solution..

This is working!!
public function handle($request, Closure $next) { if ($request->user()) { // This will return null if the user is not logged in, which evaluates to false

       if (!$request->user()->isATeamManager()) {

            return redirect('articles');
        }
    }

    return $next($request);
}

}

egom
  • 21
  • 5