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

marcow's avatar
Level 13

resolve class name

Hi everybody, I try to call Gate::inspect() with a variable class:

$this->model = 'App\Models\' . ucfirst($model_action[0]) . '::class';
// holds correct classname as string: "App\Models\City::class"
$response = Gate::inspect($this->action, App\Models\City::class);
// hardcoded Model returns allowed()=true as expected
$response = Gate::inspect($this->action, $this->model);
// variable Model returns allowed()=false

I know the problem is that $this->model is a string and the CityPolicy is never called by Gate in my variable model code. Could someone help me out how to "cast" the string value so that Gate::inspect will accept it as the Model?

Thx Marco

0 likes
2 replies
Nakov's avatar
Nakov
Best Answer
Level 73

This App\Models\City::class is not correct.. you either use City::class or use App\Models\City without the ::class as that's what the ::class returns anyway.

Please or to participate in this conversation.