0

I would like to call a model dynamically via a variable. However, it fails ( "Class "SomeClassName" not found"). Here is my code:

use App\Models\SomeClassName;
use App\Models\SomeClassName1;

class SomeClassName {
    public function store(string $someClassName, array $requestData): array
    {
        $model = $someClassName::with('relation')->findOrFail($requestData['item_id']);
        // ...
    }
}

$someClassName->store('className', []);
4
  • then $className isn't a FQCN to an existing class .... Commented Jan 11, 2023 at 17:13
  • @lagbox I have changed the names to make it more understandable / abstract. So no competition with other classes. Thank you for asking. Commented Jan 11, 2023 at 17:15
  • 1
    that wasn't a question ... $className is not a FQCN to an existing class ... why are you expecting said class to exist? Commented Jan 11, 2023 at 17:15
  • 1
    @lagbox Ok. I didn't know that. Then I will change that. I was not aware of it. Thanks for the hint. Commented Jan 11, 2023 at 17:16

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.