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

mstdmstd's avatar

How in class use model declarations ?

In laravel 9 app I make some quizzes with botman/botman 2.7 and botman/driver-web 1.5 and I have some class with botman functionality. Data are read/saved from/into db with eloquent and I have to implement it easy to move to other storage which is not supported with eloquent. For that I made all read/saved of data in separate class using interface, like :

use App\Models\{ Quiz, QuizCategory };
...
class QuizzesWizard extends Conversation
{
    /** @var Quiz Collection of active Quizzes filtered by currentQuizCategory  */
    protected $activeQuizzes;

    protected QuizCategory $currentQuizCategory;

    protected DbMethodsServiceInterface $dbMethodsServiceInterface;

    public function __construct()
    {
        $this->dbMethodsServiceInterface = App::make(DbMethodsServiceInterface::class);
    }

    public function run()
    {
        ...
        $this->activeQuizzes       = $this->dbMethodsServiceInterface::getActiveQuizzesByquizCategoryId($this->quizCategoryId, true, $this->currentLocate);
        ...
    }

    private function askNextQuizBlock(Quiz $quiz)
    {
        ...
    }

    private function getCorrectQuizAnswer(Quiz $quiz): string
    {
        ...
    }

    private function createQuizTemplate(Quiz $quiz)
    {
        ...
    }

and class to read/save data :

use App\Models\{Quiz, QuizAnswer, QuizCategory};
...
class DbMethods implements DbMethodsServiceInterface
{

    public static function getActiveQuizzesByQuizCategoryId(int $quizCategoryId, bool $shuffleData = true): Collection
    {
    ...

But it looks like that I need to remove all models in QuizzesWizard.php file, like :

   use App\Models\{ Quiz, QuizCategory };

as other implementation of DbMethodsServiceInterface would be applied without eloquent model... But I actually would like to show class in all methods like :

    private function getCorrectQuizAnswer(Quiz $quiz): string
    {
        ...
    }

not like :

  private function getCorrectQuizAnswer($quiz): string
   {

Can it be salved someway?

Thanks!

0 likes
0 replies

Please or to participate in this conversation.