6 months ago

Non-object error only when using regular function call (not with dd() )

Posted 6 months ago by chrisffm

Hey there,

I want to retrieve some data from a database through eloquent and display it in a table inside a blade view. When I call the function {{ $task->getProject()->id }}, I get the following error:

Trying to get property 'project_id' of non-object (View: XXX/XXX...).

However, when I use dd($task->getProject()->id), I do not get the error and the ID is shown. Does anybody know why this is the case?

The underlying code in the DbModel is:

    public function getProject() {

        return Project::where('id', Viewport::where('id', $this->viewport_id)->first()->project_id)->first();

Interestingly, I also do not get the error for the similar function called in the same way:

    public function getUser() {

        return User::where('id', $this->user_id)->first();

I am quite confused as the function in question has worked before and when going to the respective page, I now get the mentioned error.

Best, Christian

Please sign in or create an account to participate in this conversation.