if condition with auth() in laravel blade doesn't work - laravel

I have an is_admin column in laravel users table and want to check if user is admin or not. See below code.
#if ({{ Auth::user()->is_admin }} == true)
<li class="nav-item">
<a class="nav-link" href="{{ route('register') }}">{{ __('Register') }}</a>
</li>
#endif
This error is coming up :
syntax error, unexpected '<' (View: C:\2020\lv02\lv02\resources\views\layouts\app.blade.php)
Please help.

change your code like this:
#if (\Auth::user()->is_admin == true)
<li class="nav-item">
<a class="nav-link" href="{{ route('register') }}">{{ __('Register') }}</a>
</li>
#endif

Related

Trying to get property 'user' of non-object

in my navbar i want to allow upload link for admin role and download for user role
i got this error ...
thats my blade.php file
#if (Route::has('register'))
#if(auth::user()->user)
<li class="nav-item">
<a class="nav-link" href="{{ route('download') }}">{{ __('download') }}</a>
</li>
#endif
#else
<li class="nav-item ">
<a class="nav-link capital" href="{{ route('upload') }}">{{ __('upload') }}</a>
</a>
#endif
#endif
If you user is guest then you have no record on DB, so first check if has been authenticated or not.
change this line:
#if(auth::user()->user)
to this
#if(Auth::check())
you can use #auth #endauth
#auth
<li class="nav-item">
<a class="nav-link" href="{{ route('download') }}">{{ __('download') }}</a>
</li>
#endauth
ref link https://laravel.com/docs/8.x/blade#authentication-directives
alernative you can use optional global helper
optional(Auth::user())->$anyProperty;

Laravel 8 dissappear button

I'm trying to after the first guest registers that the register button dissappears, but i don't know how. I tried it this way but it didn't work.
#if(Auth::user())
#else
<li class="nav-item">
<a class="nav-link" href="{{ route('register') }}">{{ __('register') }}</a>
</li>
#endif
First, load all users according to your database, for example:
$users=DB::table('users')->get();
Then check the number of users, if less than 1, show register button:
#if(count($users)<1)
<li class="nav-item">
<a class="nav-link" href="{{ route('register') }}">{{ __('register') }}</a>
</li>
#endif

Laravel and MongoDB gives error Call to a member function prepare() on null

I tried to reach the web page but i get Call to a member function prepare().
what i should to do ?
I'm using laravel and MongoDB.
all codes worked good but i get above error when i updated Laravel from 5.8 to 6
<!-- Authentication Links -->
#guest
<li class="nav-item">
<a class="nav-link" href="{{ route('login') }}">{{ __('Login') }}</a>
</li>
#if (Route::has('register'))
<li class="nav-item">
<a class="nav-link" href="{{ route('register') }}">{{ __('Register') }}</a>
</li>
#endif
#else
<li class="nav-item dropdown">
<a id="navbarDropdown" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre>
{{ Auth::user()->name }} <span class="caret"></span>
</a>
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="{{ route('logout') }}"
onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
{{ __('Logout') }}
</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
#csrf
</form>
</div>
</li>
#endguest
problem is about #guest
in "vendor\laravel\framework\src\Illuminate\Foundation\Auth\User.php"
add "use Jenssegers\Mongodb\Eloquent\Model as Eloquent;"
change the "class User extends Model implements" to "class User extends Eloquent implements"
in each model use add:
"use Jenssegers\Mongodb\Eloquent\Model as Eloquent;"
and after you should extend from this eloquent instead extend Model
I've found an alternative fix.
in vendor\spatie\laravel-permission\src\Models\Permission.php and \Role.php
set this:
<?php
...
use Jenssegers\Mongodb\Auth\User as Authenticatable;
...
class Permission extends Authenticatable implements PermissionContract
{
I hope it helps you ;)

How to make different icon url in laravel breadcrump?

Hi guys i have trouble in making different icon for url using laravel breadcrump, , here the picture
I want to make icon HRMS and Dashboard is different
Here my blade file
<link rel="stylesheet" type="text/css" href="{{ asset("/css/hrms2.css") }}">
#if (count($breadcrumbs))
<ol class="breadcrumb">
#foreach ($breadcrumbs as $breadcrumb)
#if ($breadcrumb->url && !$loop->last)
<li class="breadcrumb-item" style="">
<a class="bread-color" href="{{ $breadcrumb->url }}">
<i class="fa fa-home"></i> {{ $breadcrumb->title }}</a>
</li>
#else
<li class="breadcrumb-item active">{{ $breadcrumb->title }}</li>
#endif
#endforeach
</ol>
#endif
You need to pass icon name also in $breadcrumb object and use $breadcrumb->icon instead of fa-home
<ol class="breadcrumb">
#foreach ($breadcrumbs as $breadcrumb)
#if ($breadcrumb->url && !$loop->last)
<li class="breadcrumb-item" style=""><a class="bread-color" href="{{ $breadcrumb->url }}"> <i class="fa {{ $breadcrumb->icon }}"></i> {{ $breadcrumb->title }}</a></li>
#else
<li class="breadcrumb-item active">{{ $breadcrumb->title }}</li>
#endif
#endforeach

trying to get property of non-object multi-auth

I use Hesto/multi-auth package. The username when I login success default redirect in customer.layout.auth, how can I to redirect in my blade, Example: welcome.blade.php, I can't use {{ Auth::user()->name }} in another blade, it error Trying to get property of non-object . How to fix it, please help me !
AppServiceProvider.php
public function register()
{
//
if ($this->app->environment() == 'local') {
$this->app->register('Hesto\MultiAuth\MultiAuthServiceProvider');
}
}
auth.blade.php
#if (Auth::guest())
<li>Login</li>
<li>Register</li>
#else
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
{{ Auth::user()->name }} <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="{{ url('/customer/logout') }}"
onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
Logout
</a>
<form id="logout-form" action="{{ url('/customer/logout') }}" method="POST" style="display: none;">
{{ csrf_field() }}
</form>
</li>
</ul>
</li>
#endif
I had the same problem and I think I found the solution.
You are creating your routes on the web route file.
You need to create your routes inside user route file created by Hesto/multi-auth package.
I don't know if this is the right way of doing it but it worked for me.

Resources