Thursday, April 13, 2017

object of class illuminate database eloquent collection could not be converted to int

I am accessing the user table attribute 'role' and want to check if role is 2 then show dashboard but getting this error. here is my code

protected $casts = [
    'role' => 'integer',
];

here is my controller function where I am accessing the user role column value. it returns the value in array but I want to compare it with an integer value '2'.

public function postSignIn(Request $request)
{
    $this->validate($request,[
        'email' => 'required',
        'password' => 'required',
    ]);
    $posts = Post::all();
    $email = $request['email'];
    $user = User::where("email",$email)->get(['role']);
    if(Auth:: attempt(['email' => $request['email'] , 'password' => $request['password']]))
    {
        if ($user == 2) {
            return view('admin.dashboard');

        }
        else {
            return view('frontend.layouts.user_login_layout', compact('posts'));
        }
    }else{
        return "wrong User";
    }
}



via Taiba Rani

Advertisement