1

I'm currently trying to implement a simple method on my User model:

    public function becomeAMerchant()
    {
        return $this->assignRole('merchant');
    }

However, whenever I call Auth::user() (or auth()->user()) on my MerchantController, I am unable to access this method (or any methods on the User model for that matter).

Specifically PHPStorm is telling me:

Method 'becomeAMerchant' not found on Illuminate\Contracts\Auth\Authenticatable|null

Here's my full User model:

<?php

namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    use Notifiable, SoftDeletes, HasRoles;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function itineraries()
    {
        return $this->hasMany(Itinerary::class);
    }

    public function becomeAMerchant()
    {
        return $this->assignRole('merchant');
    }

    public function isMerchant()
    {
        return $this->hasRole('merchant');
    }

}

As a quick background, I'm using Spatie's laravel-permission package to manage roles and permissions thus far.

Can anyone help me figure out why I'm unable to access the methods on my User model?

1 Answer 1

2

That is because Auth::user() returns an Authenticatable: this is very likely your User model, but it is not guaranteed to be - so PHPStorm warns you. It's also possible it returns null, for example when no user has been authenticated.

Therefore, you should check what type of object you have at hand before accessing any methods specific to your User model.

$user = Auth::user();

if ($user !== null && $user instanceof App\Models\User) {
   $user->becomeAMerchant();
}

Or:

$user = Auth::user();

if ($user !== null && method_exists($user, 'becomeAMerchant')) {
   $user->becomeAMerchant();
}

Finally - if you are 100% sure what the type of an object is, you can tell PHPStorm as follows:

/** @var App\Models\User $user */
$user = Auth::user();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much for the response! One thing I've noticed is that methods called on the User model are not taking effect, it's not just PHPStorm. For example, I have access to methods such as getAuthIdentifier() and getRememberToken(), but otherwise the application isn't throwing an error of any sort, it's just ignoring the User methods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.