Doesn't is work with just
:\Illuminate\Contracts\View\View
If so you can import it
use Illuminate\Contracts\View\View;
public function index(): View
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I would like to type hint my controller methods like this:
public function index(): \Illuminate\View\View;
{
$users = User::orderBy('created_at', 'desc')->paginate(30);
return view('logs.users.index', compact('users'));
}
Although this works, my PHPStorm editor will complain with this:
Return value is expected to be '\Illuminate\View\View', '\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View' returned
I would need to change it to
public function index(): \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
{
$users = User::orderBy('created_at', 'desc')->paginate(30);
return view('logs.users.index', compact('users'));
}
To keep my IDE happy. But I feel like this looks really messy. Especially as I have to do it for every controller method.
How are you doing it? Is there a better alternative? Or do you just suppress those kinds of warnings?
Please or to participate in this conversation.