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

bigweld86's avatar

Php 7.1 Return Type-hint failing in Laravel 5.7

I just cloned a repository for a project I was working on. Don't quite remember the specifics of the previous machine it was running on but on this one it's using php 7.1 and I upgraded to Laravel 5.7. The issue is that in my previous machine, this piece of code was working:

class ProductsController extends Controller
{
    public function index() : Object
    {
        $products = Product::all();
        return view('products.index', ['products' => $products]);
    }
}

Notice the Object return type.

After running migrations and everything else, when accessing index I'm getting the following error in the new machine:

Return value of App\Http\Controllers\ProductsController::index() must be an instance of App\Http\Controllers\Object, instance of Illuminate\View\View returned

which is related to the return type hint being Object. How do I know that? Because if I remove it everything works like charm.

Why is that happening?

0 likes
6 replies
bigweld86's avatar

@SERGIU17 - When I run:

gettype(view('products.index', ['products' => $products])) 

i get back 'object'. You're right, view() returns an instance of view which is basically an object. But ok, how do I enforce the return type to be an 'instance of View'? Again, this was working before, not sure why changed so it's breaking now :(

Sergiu17's avatar

@BIGWELD86 -

/**
 * @return View
 */
public function index(): View
{
    $products = Product::all();

    return view('products.index', ['products' => $products]);
}

Like so

bigweld86's avatar

@SERGIU17 - Nope, that's not going to work, I tried it already and I got the same error:

Return value of App\Http\Controllers\ProductsController::index() must be an instance of App\Http\Controllers\View, instance of Illuminate\View\View returned

Please or to participate in this conversation.