Home > front end >  How can I run request with model name and filter set as strings?
How can I run request with model name and filter set as strings?

Time:01-23

How on laravel 9 site can I run request with model name and filter set as strings :

runRequest(model: \App\Models\Vote::class, filter:"status = 'A'");


public function runRequest(string $model, string $filter)
{
        $data = $model::whereRaw($filter)
            ->orderBy('id', 'asc')
            ->get();
}

But error was raised :

 Class name must be a valid object or a string

How to make it working?

"laravel/framework": "^9.19",

Thanks in advance!

CodePudding user response:

use this

   App::make($model)
           ->query()
            ->whereRaw($filter)
            ->orderBy('id', 'asc')
            ->get()
  • Related