Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

panthro's avatar

Using a model by referencing a config file?

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?

0 likes
28 replies
tykus's avatar

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

1 like
tykus's avatar

@panthro resolve is an alias for app

// vendor/laravel/framework/src/Illuminate/Foundation/helpers.php
function resolve($name, array $parameters = [])
{
      return app($name, $parameters);
}
1 like
panthro's avatar

@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?

tykus's avatar

@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);
panthro's avatar

@tykus ah yes, that just wraps new...

Is there any issues in using new on a resolved class?

Sinnbeck's avatar

@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

tykus's avatar

@panthro I don’t know what you’re asking; you are getting a new instance whenever you resolve from the container

panthro's avatar

@tykus

$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?

panthro's avatar

@Sinnbeck i dont know why you would call new im hunting for ways to resolve a class and then create a new model.

tykus's avatar

@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.

panthro's avatar

@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's avatar

@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();.

tykus's avatar

@panthro no idea what you're at here; can you show how your Service class constructor looks.

panthro's avatar

@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()
{
    //??
}}

Sinnbeck's avatar

@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's avatar

@Sinnbeck that's possible but does not solve the issue, your store method is going against what you said earlier:

@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

You are resolving, then create is also making a new version of it.

tykus's avatar

@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).

Sinnbeck's avatar

@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

panthro's avatar

@tykus i think on store you meant... $this->model->create($attributes);

tykus's avatar

@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's avatar

@tykus the issue with new is

@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

Sinnbeck's avatar

@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

panthro's avatar

@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();
tykus's avatar
tykus
Best Answer
Level 104

@panthro like I have shown you already:

$model = resolve(Config::get('comments.model'));
$model->all();

$model = $model->fill($attributes);
$model->save();
1 like

Please or to participate in this conversation.