0

I have a strange behavior in naming model relationship in my laravel application (Laravel 9, PHP 8.1)

I have two models

class User extends Model
{
    public function userSocialProviders()
    {
        return $this->hasMany(UserSocialProvider::class);
    }
}
class UserSocialProviders extends Model
{
    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

When trying to fetch the relation using dynamic property it returns null

dd($user->userSocialProviders);   // null

it only works if I renamed the method to providers

class User extends Model
{
    public function providers()
    {
        return $this->hasMany(UserSocialProvider::class);
    }
}

then it returns the desired result

dd($user->providers);   // Collection<UserSocialProvider>

I don't know the reason behind that and of course it's a very bad name. $user->providers doesn't mean anything related to socialAccounts

any help How to change the method name and still use the dynamic properies ?

0 Answers0