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

sohexaxok's avatar

Project to PHP 7.1

Hi,

I'm upgrading a Laravel 5.6 project to PHP 7.1, but I get this error: Return value must be an instance of App\Http\Controllers\Object, instance of Illuminate\View\View returned

class ComputersController extends Controller
{
    public function index() : Object
    {
        $computers = Computer::all();

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

Any idea why I get this error? View is object as well right?

0 likes
12 replies
bobbybouwmann's avatar
Level 88

Object itself is not a valid keyword in PHP. You do have the lower key version for that though: object. Anyway, with PHP the return type should match the actual class, otherwise strict typing wouldn't make sense!

So you should update it to this

use Illuminate\View\View;

class ComputersController extends Controller
{
    public function index(): View
    {
        $computers = Computer::all();

        return view('computers.index', compact('computers'));
    }
}

Let me know if that makes sense to you ;)

1 like
jlrdw's avatar

Wouldn't

: View

be totally unnecessary.

jlrdw's avatar

I know about return types but since when is view a return type.

Float as in the link you gave would be a return type.

I am sure that the view is handled by Taylor in the vendor folder.

Putting that colon View does nothing.

I'm surprised an error wasn't encounted.

Cronix's avatar

Putting that colon View does nothing.

Sure it does, if you return something other than a view class you'll get an error because it won't match the specific class you told it to expect returned. It doesn't trigger an error because it's correct. Try it. It takes 30 seconds. Tell it you're returning a view class, but return a request object instead or something other than a view.

use Illuminate\View\View;

public function index(): View
{
    return view('someView');
}

the above works fine

This will error out

use Illuminate\View\View;

public function index(): array
{
    return view('someView');
}

Return value of App\Http\Controllers\BlahController::index() must be of the type array, object returned

Hmm, let's try returning $request instead of a view

use Illuminate\View\View;
use Illuminate\Http\Request;

public function index(Request $request): View
{
    return $request;
}

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

That doesn't work either. Have to return the exact class you said it was going to be...

1 like
jlrdw's avatar

public function index(): array

I agree with that.

But I am saying : view is not needed or required to return a view.

That's over use. Use the return types when needed, not just for the yek of it.

Taylor has handled that already.

Cronix's avatar

Great, that doesn't have a damn thing to do with what they are asking. Thanks for your opinion that it's not needed. It's not up to you. It's not your project, and not what they asked.

jlrdw's avatar

No comment on that, but I do not believe in condoning mis use of something.

It's not your project

Well when we advise someone of (just example here) that storing credit cards in their system isn't correct, the service does that. That is also not mine or your project, but we attempt to give the best advice we can.

So using : view just for the yek of it when the OP is clearly returning a view anyway the way the docs show to is over use of the intent of the return type.

And yes that is just my opinion, that it's bad practice.

If ensuring an array, that's okay.

I know already that

Well when we advise someone of (just example here) that storing credit cards ...

Has nothing to do with this question. But it does, I was merely giving an opinion and the credit card thing is an analogy of an answer we might give to help.

Please don't read things into something that isn't there.

@sohexaxok do whatever you want, but honest to return a view like:

return view('computers.index', compact('computers'));

The added : View is not needed. It's redundant. Which is all I was trying to say.

sohexaxok's avatar

I just wanted to know the return type! @bobbybouwmann helped me out with that.

@jlrdw You seem to talk about a lot of different things which makes this really hard for me... I just want to set the return type to make sure I return the correct kind of object

jlrdw's avatar

@sohexaxok what you are doing is great for custom classes and if you are a framework writer.

I was merely saying that is not required where you are using it, if you are returning a view Taylor has taken care of that for you already.

I have not seen one video or anywhere in the documentation where a colon view is required.

That's all.

I guess I am confused on how you cannot understand that.

Cronix's avatar

I was merely saying that is not required where you are using it

I guess I am confused on how you cannot understand that.

No one said it was required. It's an optional feature for php 7.1+ that has nothing to do with laravel, or the points you brought up here. Maybe that's part of your confusion.

bobbybouwmann's avatar

@jlrdw Don't be so naive. This guy just had a question about a specific return type and you start talking about things not being required. We all know that..! Your opinion on when to use the return types is not relevant for the current question :D

Please or to participate in this conversation.