Я Диша
Я Диша

Reputation: 1

Attempt to read property "id" on null

Some time I got this error and dissappears again when I make artisan optimize and also for browser. How can solve it please?

Ecommerce_Laravel8\app\Http\Repositories\UserRepository.php:32

namespace App\Http\Repositories;

use App\Http\Interfaces\UserInterface;
use App\Models\User; 
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class UserRepository implements UserInterface {

private $userModel;

public function __construct(User $user){

    $this->userModel = $user;
}  //end Method


public function logout()
{
    Auth::logout();
    return redirect()->route('login');
}  //end Method

public function profile()
{
    $id = Auth::user()->id;
    $user = $this->userModel::find($id);

    return view('user.profile.view_profile',compact('user'));
}  //end Method` ##

Upvotes: 0

Views: 4005

Answers (2)

Murtaza Noori
Murtaza Noori

Reputation: 91

so what youre doing is wrong when youre logging out youre still calling the Auth::user()->id which does not exist anymore when you log out because your connection to database is removed but what you can do is , to simply remove the Auth::user()->id and make a redirect route back to the login page , so when you logout you can go back to login page or anyother page you want you have redirect it to that

Upvotes: 0

akinakalin
akinakalin

Reputation: 66

Why are you get users twice? Auth::user() is the user you want anyway.

public function profile()
{
    $user = Auth::user();

    return view('user.profile.view_profile',compact('user'));
}  

Upvotes: 1

Related Questions