0

I made a command in which I have to pass my model name to process some data. How can I call it from the controller as I can't mention it as App\Models\{ModelName} because in controller the model's name is passed as a string.

Basically my doubt is, if in a function of a controller my model's name is passed as a string, how can I fetch it?

2
  • You mean you want to get the model's instance in a method to work with it?? Commented Mar 31, 2021 at 6:49
  • @Saud yes exactly Commented Mar 31, 2021 at 6:55

2 Answers 2

1

You can do it like this. And no laravel won't consider it as a string.

$namespace = 'App\\Models\\';
$model = $namespace.'Foo';

$result = $model::where('status', 1)->get();
Sign up to request clarification or add additional context in comments.

Comments

0

Even if the model goes in the text, you can use it like this

$class = 'App\Models\User';

return $class::find(1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.