In laravel Blade templating we can exclude some parts of HTML with this code:
#if (Auth::user())
<li>Mein Profil</li>
<li>Admin</li>
#else
<li>Mein Profil</li>
#endif
If user is authenticated then show home and admin links and if user is not authenticated then show only home link.
My question is how to make a check here if user is admin?
I have default login system from laravel and i just added one more column in table users -> ('admin') with tinyint value 1 and in this video
https://www.youtube.com/watch?v=tbNKRr97uVs
i found the code for checking if user is admin
if (!Auth::guest() && Auth::user()->admin )
and it works in AdminMiddleware.php
but it doesn't work in blade. How to make this working??
I find such a long winded, check if logged in, check role, being added all around my blade files to distracting. You may consider adding a custom blade directive. Add something like this to AppServiceProvider boot() function
Blade::if('admin', function () {
return auth()?->user()?->admin === true;
});
in blade just use
#admin
<p>Only admin sees this</p>
#endadmin
#if (!Auth::guest() && Auth::user()->admin)
<li>Mein Profil</li>
<li>Admin</li>
#else
<li>Mein Profil</li>
#endif
this works just to be clear (just add one more column tinyint 'admin' in user table and set to 1)
You can use the method> HasRole
You can call this from artisan tinker from command line, just to see it before writing code:
php artisan tinker
Check if the user with id 1 has role 'user';
User::find(1)->hasRole('user');
Check if the user with id 1 has role 'admin';
User::find(1)->hasRole('admin');
The same way while coding for example in app.blade template:
#if (Auth::user()->hasRole('admin'))
<li>Main Profile</li>
<li>Admin</li>
#else
<li>Main Profile</li>
#endif
The method hasRoles comes from the package spatie/laravel-permission
composer.json:
"spatie/laravel-permission": "^3.16"
full path of the class:
vendor\spatie\laravel-permission\src\Traits\HasRoles.php
Related
I am new in Laravel. I include Header using #include('header'). but I want to hide profile menu when user is not register. How can I do ?
I use this to get data from table directly in blade.php
$array = App\Table::Select(users)->get();
You can use the Auth facade to determine if the current user is logged in:
header.blade.php
#if(Illuminate\Support\Facades\Auth::check())
<!-- profile menu -->
#endif
Edit: You can use blade directives except the #if to checking user is authenticated or not. like the following:
#auth
<!-- Profile Menu -->
#endauth
If you—for whatever reason— just want to know if some user is registered, change your query to the following:
#if(Profile::find($userId) !== null)
<!-- profile menu -->
#endif
You can use auth facade for this.
#auth // The user is authenticated... #endauth
#guest // The user is not authenticated... #endguest
Simple use this
if(Auth::user())
include('header')
endif
I am using laravel auth application in that layout page is present, So to get name of current logged-in user in layout.blade.php I have use {{Auth :: user () -> name}} and it gives name too. But the problem is that when i get logout and try to login then login page gives error Trying to get property 'name' of non-object.
Please help me.
If no user is logged in so Auth::user() will return null,
so in your blade you can use Auth::check() to verify if a user is logged in
#if(Auth::check())
{{ Auth::user()->name }}
#endif
More info can found in
https://laravel.com/docs/7.x/authentication#retrieving-the-authenticated-user
you can use #auth #endauth helper in blade
#auth
{{ Auth::user()->name }}
#endauth
ref link https://laravel.com/docs/8.x/blade#authentication-directives
You will need to check if User is logged in first
try
#if(auth()->check())
{{auth()->user()->name}}
#endif
or in one line
{{auth()->user()->name ?? ''}}
I need to filter my navbar upon a column in my users table which is user_type.
user type column has student, Coordinator and admin.
I am able to restrict the buttons of my navbar to only authenticated users using
#if(!Auth::guest())
<li>My projects</li>
#endif
but I am trying to show some buttons only from the Coordinator, how I am able to do that ?
Try this
#if(Auth::check())
#if(Auth::user()->user_type == 'Admin')
<li>Admin Panel</li>
// your logic
#elseif(Auth::user()->user_type == 'Coordinator')
// your logic
#else
// your logic
#endif
#else
// your logic
#endif
What is the best way to create a main menu in Laravel 5? And how to show menu items only when an user is logged-in? And What is the best way to make this multi language?
Laravel provides an easy way to check if the user is logged-in by using the facade Auth::check().
if (Auth::check()) {
// The user is logged in...
}
About the translation, you can check here: Localization
The structure is defined like this, as per documentation:
/resources
/lang
/en
messages.php
/es
messages.php
Laravel also provides an easy way to translate phrases using the trans('string.to.translate'), which can be seen here trans().
Inside messages.php (in both lang directories), you must set the translation string. In en/messages.php:
return [
'welcome' => 'Welcome'
];
In es/messages.php:
return [
'welcome' => 'Bienvenido'
];
With these two, you can do the following in you application for example:
// Get the user locale, for the sake of clarity, I'll use a fixed string.
// Make sure is the same as the directory under lang.
App::setLocale('en');
Inside your view:
// Using blade, we check if the user is logged in.
// If he is, we show 'Welcome" in the menu. If the lang is set to
// 'es', then it will show "Bienvenido".
#if (Auth::check())
<ul>
<li> {{ trans('messages.welcome') }} </li>
</ul>
#endif
Try with https://laravel.com/docs/5.1/authentication
For example:
if (Auth::check()) {
return something to view
}
I saw this question. I have a similar problem, but it ain't working. Laravel 4 public functions.
I have a view composer that includes some codes in base layout. Here's my composer:
View::composer(array('common.menu_addition','common.base_errors','common.view_special'), function($view)
{
if(Auth::check()) {
$roles = Auth::user()->type;
if ($roles == '5') {
$view->with('roles', $roles);
} else {
return Redirect::to('news/index');
}
}
});
When I'm not logged in, it works perfectly. But one of my files of view composer goes like this:
<div class="pull-right">
#if (Auth::check())
#if ($roles == 5)
<ul class="nav">
<li>
<a href="{{ URL::to('admin/dash') }}">
<i class="icon-eye-open">
</i>
<strong>
Admin Dashboard
</strong>
</a>
</li>
</ul>
#endif
#endif
</div>
When I login, it won't display any site. It just says Undefined variable: roles in that view composer file.
I don't think it's possible to return a redirect like that, in your view composer. And even if you could it's a bad place to put that logic. You should create a filter http://four.laravel.com/docs/routing and redirect away if the user doesn't have the proper authentication level.
It looks like you're not defining $roles beforehand and not passing $roles into your views.
When you're making checks against the $roles in views you need to make sure that variable actually exists. Other possibility is to change the check to something like
#if (isset($roles) && $roles == 5)
to ignore the error but that gets really messy in the long run.
So make sure to initialise view variables beforehand in the view composer and everywhere else. Laravel tries to be much more stricter about non-initialised variables than PHP in common.