简体   繁体   中英

How to use IF statement with $id = Auth::id(); to get users id Laravel?

I've searched everywhere for this and tried to make it happen all day today, but still can't figure it out.

Basically I am trying to use IF statements in routes to display appropriate info to users with appropriate ID.

But I am getting

Function name must be a string

For this

if ($user = $id(8))

I can't figure out how to pass in current users id to that if statement to check it. Sorry if dumb question, but I'm just starting with Laravel.

Route::get('/secret', function()
{
$user = Auth::user();
$id = Auth::id();

if ($user = $id(8))
{
    return 'It worked!';
}

return 'Your id is not 8.';
});

Thanks

You cannot use if($user = $id(8)) , try to use: if($id == 8) instead. You want to compare two values, so you need to use == , = means assigning a value.

Hope it works! By the way: you don't have to use the $user variable as far as I can see. $id is enough in your case ;)

JohnLV's answer is correct, but you look like you might be better off making a route filter - read here for more information:

http://laravel.com/docs/routing#route-filters

This is for when you want to block access to certain pages/routes based on user privileges (ie hide admin area from users).

But if you want to change things in you view based on user id, then it is best to put this code in the controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM