Home > Enterprise >  If statment on Data without using $request variable
If statment on Data without using $request variable

Time:12-30

i'm trying check if the user is an Admin so i can restrict that URL from other users, so far i tried this code :

 public function Adminp(){
    $data = ['LoggedUserInfo' =>Utilisateurs::where('id','=',session('LoggedUser'))->first() ];
    $utilisateurs = Utilisateurs::all();
    $utilisateur->role = $_GET['role'];
    if($utilisateur->role == 'Admin'){
        return view('Admin.admin-dashboard', $data, compact('utilisateurs'));
    }
    else{
        return abort(404);
     }
}

all i get are errors

CodePudding user response:

I am just guessing at what you are doing. I will assume you just want to see if the User that you retrieved from the database has the role you are looking for ... you should be using the authentication system for this and a Middleware to do the filtering, but

public function Adminp()
{
    $user = Utilisateurs::findOrFail(session('LoggedUser'));

    if ($user->role == 'Admin') {
        return view('Admin.admin-dashboard', [
            'LoggedUserInfo' => $user,
            'utilisateurs' => Utilisateurs::all(),
        ]);
    }

    return abort(404);
}

Let me stress this again though, you should be using the Authentication system and a Middleware to do the filtering instead of reinventing the wheel.

CodePudding user response:

$utilisateurs = Utilisateurs::all();

This will return you array of Utilisateurs and in the next line you are trying to get role property that will never exist in that array you need to get only one user not the array of all users.

  • Related