Resolve the class out of the Controller:
app(config('my-package.model'))->all()
Storing anything other than the FQCN in config will be detrimental in the long run
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I store a reference to my model inside config;
return [
'model' => MyCompany\MyPackage\Comment::class,
];
I want to use this in my class, for example, to get all comments:
Config::get('my-package.model')->all();
The above does not work as all is operating on a string.
So with that in mind, how should I store class references in a config? And how should I use them in classes?
I've considered: resolve(Config::get(my-package.model')) and new Config::get(my-package.model').
I would appreciate advice on the best route here?
Resolve the class out of the Controller:
app(config('my-package.model'))->all()
Storing anything other than the FQCN in config will be detrimental in the long run
@tykus what is the difference between app and resolve?
@panthro resolve is an alias for app
// vendor/laravel/framework/src/Illuminate/Foundation/helpers.php
function resolve($name, array $parameters = [])
{
return app($name, $parameters);
}
@tykus lovely thanks for that information.
Can I ask this follow up:
I do this I will get all comments:
$model = resolve('Config::get(my-package.model'));
$model->all();
But when I want to create a comment I have to new the resolved class:
$model = resolve('Config::get(my-package.model'));
$comment = new $model($data);
// add various relations
$comment->save();
Is this correct or is there a better way?
@panthro you can use the create method:
$model = resolve('Config::get(my-package.model'));
$comment = $model::create($data);
or inline it:
$comment = resolve('Config::get(my-package.model'))->create($data);
@tykus ah yes, that just wraps new...
Is there any issues in using new on a resolved class?
@panthro why would you call new? As long as it isn't a singleton, you will get a new instance each time you resolve it
@panthro I don’t know what you’re asking; you are getting a new instance whenever you resolve from the container
$comment = resolve('Config::get(my-package.model'))->create($data);
This would resolve it and new it though as create also news a class.
How would/should you use resolve when making a model?
@Sinnbeck i dont know why you would call new im hunting for ways to resolve a class and then create a new model.
@panthro you can pass an array of parameters to the resolve method. For a model, the parameter should be an attributes array:
$comment = resolve(Config::get(‘my-package.model'), ['attributes' => $data]);
This will give you a new in-memory instance which you will need to save.
@tykus interesting, but what if I resolve the config class in my constructor so it can be used for creating or looking up models?
$this->model = resolve('Config::get(my-package.model'));
I can then get all:
$this->model->all();
How can I make a new model for DB insertion with the resolved instance without using new? The create method you mentioned earlier utalizes new.
@panthro what are you talking about 🤔
@tykus I want to resolve the class in the constructor of my service class, then use it throughout the class, what you suggest....
$comment = resolve(Config::get(‘my-package.model'), ['attributes' => $data]);
Will not work when you want to do $comment->all();.
@panthro no idea what you're at here; can you show how your Service class constructor looks.
@tykus sure, I'm not sure what to do for store
public $model;
public function __construct()
{
$this->model = resolve(Config::get('comments.model'));
}
public function all()
{
return $this->model->all();
}
public function store()
{
//??
}}
@panthro why not just build it where needed?
protected function getModel()
{
return resolve(Config::get('comments.model'));
}
public function all()
{
return $this->getModel()->get();
}
public function store()
{
$this->getModel()->create([]);
}}
@panthro I don't suppose you are getting closer to an answer here...
public function store($attributes = [])
{
$this->model->create($attributes);
}
If you think you need to; you can extract a method to resolve a separate instance in each of the actions:
public $model;
public function __construct()
{
$this->model = Config::get('comments.model');
}
private function getInstance()
{
return resolve($this->model);
}
public function all()
{
return $this->getInstance()->all();
}
public function store($attributes = [])
{
$this->getInstance()->create($attributes);
}
Ultimately; I don't know why you are fixating on new - what is the problem you are trying to solve here? new does not persist an Model instance (there is no database record created).
@panthro yes it is making a new version of it. But I would find that fine as you are using it in different contexts.
I was referring to not manually calling new
@tykus i think on store you meant... $this->model->create($attributes);
@Sinnbeck oh so new would not cause any issues?
@panthro not in the bigger snippet; $this->model is a string there - you need to get an instance to work with in store. If you prefer to use the same instance:
public function store($attributes = [])
{
$model = $this->getInstance()->fill($attributes);
$model->save();
}
You didn't explain your issue with new???
@panthro I don't understand; what is the issue here?
@panthro calling resolve more than once isn't a problem. I was only referring to you not needing manually calling new
Example
//what I would do
$model = resolve(Config::get('comments.model'));
$model2 = resolve(Config::get('comments.model'))
//what I wouldn't do
$model = resolve(Config::get('comments.model'));
$model2 = new get_class($model);
But explain what the current problem is. I am also confused
@Sinnbeck basically I want to know if I can / the best way to:
$model = resolve(Config::get('comments.model'));
//get all models
$model->all();
// save a model
$model = new $model($attributes);
// add relationships
$model->save();
@panthro like I have shown you already:
$model = resolve(Config::get('comments.model'));
$model->all();
$model = $model->fill($attributes);
$model->save();
Please or to participate in this conversation.