Trying to get property 'user' of non-object - laravel

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;

Related

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

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

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

How are the "Login" and "Register" links from Laravel's built-in authentication system added to existing pages?

I have created several pieces of my Laravel 7 application (I'm a newbie trying to learn). I then wanted to add authentication on top of that, so I did:
php artisan ui bootstrap --auth
npm install && npm run dev
After doing so, I have a "login" and a "register" link on the top left-corner of every page of my app. These links are being added by some automated method and are not changing the pages (views - *.blade.php files) that I had created before adding the authentication system.
I'd like to prevent these links from being added automatically. Then I can add my own links to my navigation bar. The problem is I can't figure out how they're being created or added to my views. Can someone help me?
NEVERMIND - I FIGURED IT OUT.
Appearently during the authentication system scaffolding my "layout.blade.php" was modified to include both the "login" and "register" links by the addition of this code just under the <body> tag:
<div class="flex-center position-ref full-height">
#if (Route::has('login'))
<div class="top-right links">
#auth
Home
#else
Login
#if (Route::has('register'))
Register
#endif
#endauth
</div>
#endif
</div>
All I needed to do is remove that code block. Hope this helps someone in the future.
The login links are created on a separate template file (views/layouts/app.blade.php). To remove them you need to customize this file.
This is the part where the header links are created:
<!-- Right Side Of Navbar -->
<ul class="navbar-nav ml-auto">
<!-- 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
</ul>

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 ;)

I need to hide an specific element of a navbar depending on a rol

I have a navbar and I have 2 elements outside the login option, the thing is that I need that a certain element stays with a certain role and when I'm not logged in...
Here is my code
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="#services"><i class="fa fa-search"></i> Buscar</a>
</li>
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="#about">Explorar cursos</a>
</li>
#if (Route::has('login'))
#auth
#role('Alumno')
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="#about">Explorar cursos</a>
</li>
<li class="nav-item">
<a class="nav-link js-scroll-trigger" href="{{route('miscursos')}}"> Mis cursos</a>
</li>
#endrole
#role('Administrador')
<li class="nav-item dropdown">
<a class="nav-link" href="{{ route('home') }}" >
{{ __('Tablero')}}
</a>
</li>
#endrole
The second element on the list is the one I'm having problem with because I want it when I'm browsing in the page but NOT logged in and I need when I'm logged in with an 'Alumno' role
The thing is that I don't wanna see it when I'm logged in as an administrator
You can use :
Auth::guest() for conditional when user it not logged in. So if I am getting it right, in your second list element :
<?php
#if(Auth::guest() || Auth::user()->role != 'Administrator)
// Only visible if user is NOT logged in OR logged in but not an administrator
#endif

Resources