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
awcodes
awcodes5mo ago
might be better to bind the class in a service provider on in the packageRegistered method if you are using Spatie's Package Tools.
app()->bind(User::class, config('plugin.user_model'))
app()->bind(User::class, config('plugin.user_model'))
Schilly
Schilly5mo ago
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
Schilly
Schilly5mo ago
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
use App\Models\User;
use App\Models\User;
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
Schilly
Schilly5mo ago
as in the function:
/**
* Create or update a user in the database.
*
* @throws Exception
*/
public function createOrUpdateUser(\Pschilly\Larascord\Types\User $user): User
{
if (!$user->getAccessToken()) {
throw new Exception('User access token is missing.');
}

if (Schema::hasColumn('users', 'deleted_at')) {
return User::withTrashed()->updateOrCreate(
[
'id' => $user->id,
],
$user->toArray(),
);
}

return User::updateOrCreate(
[
'id' => $user->id,
],
$user->toArray(),
);
}
/**
* Create or update a user in the database.
*
* @throws Exception
*/
public function createOrUpdateUser(\Pschilly\Larascord\Types\User $user): User
{
if (!$user->getAccessToken()) {
throw new Exception('User access token is missing.');
}

if (Schema::hasColumn('users', 'deleted_at')) {
return User::withTrashed()->updateOrCreate(
[
'id' => $user->id,
],
$user->toArray(),
);
}

return User::updateOrCreate(
[
'id' => $user->id,
],
$user->toArray(),
);
}
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:
<?php

namespace App\Models;

use App\Models\S1Personnel\User as S1PersonnelUser;

class User extends S1PersonnelUser
{ }
<?php

namespace App\Models;

use App\Models\S1Personnel\User as S1PersonnelUser;

class User extends S1PersonnelUser
{ }
awcodes
awcodes5mo ago
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.
Schilly
Schilly5mo ago
Very strange... As when I did that it just kept saying that the user model doesn't exist. No clue 😦 thanks for help though