dynamic Use $class;
I am working on making a plugin / module / whatever you want to call it...
I am curious to know if it is possible to dynamically call a model name based off a config value.
EG: config value is:
USER_MODEL = "\App\Model\User"
and in the model class I want to be able to dynamically set that for the:
Use $user_model;
part at the top..
I am not sure if this is even possible or not?6 Replies
might be better to bind the class in a service provider on in the packageRegistered method if you are using Spatie's Package Tools.
Hmm. I was looking at that and couldn't get it to behave.
I am trying to adapt a custom fork of Larascord to my needs - but also don't want to have it be so specific that no one else could use it. Right now in the package there is a "Discord service" that handles all the user creation, etc... which works fine if your user model is App/Models/User but nothing else
I'm not at desk now - I'll post some snippets when I get back to make it more clear
So what I am trying to do exactly is this. In this file - https://github.com/pschilly/larascord/blob/main/src/Services/DiscordService.php - it processes the user login and either makes or updates the user record after authenticating via OAuth2.0. The issue is - I want to be able to dynamically refer to the so that people can change a config value to whateaver they use in their app.
GitHub
larascord/src/Services/DiscordService.php at main · pschilly/larasc...
Larascord is a package that allows you to authenticate users in your Laravel application using Discord. - pschilly/larascord
as in the function: it refers to that model - but I cannot seem to figure out how to dynamically load the right model based on config (.env) value
was able to get it to work using this as an intermediate... Not the cleanest approach but works I suppose:
Glad you found a solution. But binding to the class should still work. I’m doing it in curator and it’s working fine.
As long as the class in the config extends User, it should be fine.
Very strange... As when I did that it just kept saying that the user model doesn't exist. No clue 😦 thanks for help though