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

tjkalinowski's avatar

Production not works = same code on dev works :)

Hello!

I have spend 2h searching what wrong, and found zero....

When I call URL http://production.com/comments/index I see that error.

3/3
ErrorException in 1dea0486404fc5ccd0169877e8bb3cc4003b56ab.php line 8:
Trying to get property of non-object (View: /home/aknsvip/domains/production/resources/views/comments/comment.blade.php) (View: /home/aknsvip/domains/production/resources/views/comments/comment.blade.php)

1. in 1dea0486404fc5ccd0169877e8bb3cc4003b56ab.php line 8
2. at CompilerEngine->handleViewException(object(ErrorException), 1) in PhpEngine.php line 44
3. at PhpEngine->evaluatePath('/home/aknsvip/domains/production/storage/framework/views/819ed1c26b86a6fe11ece353533e8ada68efd606.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'comments' => object(LengthAwarePaginator), 'CommentErrorCheck' => null, 'CommentError' => null)) in CompilerEngine.php line 59

Nothing new, very common error. Lets see. In 1dea0486404fc5ccd0169877e8bb3cc4003b56ab.php in line 8 I see this:

<tr>
    <td> 
        <code> 
                <?php echo e($comment->body); ?> 
        </code> 
        <br>
        <font size="-1">
                <B> <?php echo e($comment->event->type->name); ?> </B>    <-----this
                <i><?php echo e($comment->user->name); ?> -> (<?php echo e($comment->updated_at); ?>) </i>      <------this
        </font> 
    </td>
    <td>
            <?php if( Auth::user()->role == "Administrator"): ?>
                <a href="/events/<?php echo e($comment->event->id); ?>/show" class="btn btn-info btn-sm active" role="button" aria-pressed="true">Info</a>     <----this     
                <a href="/comments/<?php echo e($comment->id); ?>/destroy" class="btn btn-danger btn-sm active" role="button" aria-pressed="true">del</a>
            <?php endif; ?>
    </td>
</tr>

When I manually remove lines marekt <----this error goes off. And page loaded.

WHY???

I see that errror IS related to calling many to on relation, like in this example :

 e($comment->event->type->name);

I have found original file in Comments/index.blade.html this is it;

        <font size="-1">
                <B> {{ $comment->event->type->name }} </B>
                <i>{{ $comment->user->name }} -> ({{ $comment->updated_at}}) </i>
        </font> 

When I remove this line in my source code, production works.

Tell me why my Production not working with this code, but my development works. I have the same source code on both servers.

Where can I search for help?

This relationships are very vell linked in Comments, Events models. And works in other part of software.

0 likes
11 replies
tykus's avatar

You are missing an object somewhere in $comment->event->type->name - one of the relations is returning null which is not uncommon. Better to code defensively, and test for existence of the object before using it.

tjkalinowski's avatar

My relations is ok.

Comment.php:

    public function event() 
    {
        return $this->belongsTo(Event::class);
    } 

Event.php

public function comment()
    {
        return $this->hasMany(Comment::class);
    } 

    public function type() 
    {
        return $this->belongsTo(Type::class);
    }  

Type.php

  public function event()
    {
        return $this->hasMany(Event::class);
    }        

I do not see wrong relations here. hmmm....

tjkalinowski's avatar

I have found that this line works in CommentsController.php

echo "<BR><BR><BR>"; var_dump($comments[0]->event->type->name);

But the same not works in comments/index.blade.php

<B> {{ $comment->event->type->name}} </B>

VERY strange.....

willvincent's avatar

Presumably you're looping through each comments as comment prior to the errant line.

There is obviously a comment in the collection where event is not an object. That's your problem. As has been said, it's not strange.

tjkalinowski's avatar
WHY ***THE CODE** WORKS on development laptop?
WHY ***THE CODE** WORKS on development laptop?
WHY ***THE CODE** WORKS on development laptop?
WHY ***THE CODE** WORKS on development laptop?

WHY NOT on production hosting?
WHY NOT on production hosting?
WHY NOT on production hosting?
WHY NOT on production hosting?

I am looping here :

index.blade.php

                        <tbody>
                                @foreach($comments as $comment)
                                        @include ('comments.comment')
                                @endforeach
                        </tbody>

comment.blade.php

    <td> 
        <code> 
                {{$comment->body }} 
        </code> 
        <br>
        <font size="-1">
                <B> {{ $comment->event->type->name}} </B>   <---LINE 8
                <i>{{ $comment->user->name }} -> ({{ $comment->updated_at}}) </i>
        </font> 
    </td>
    <td>

When I change LINE 8 to this:

{{ $comment->anything }}  <----this works
{{ $comment->event->type->name }} <---this not

anything = any column of comments table. This works.

Does my production server has too small memory to hold huge object?

mazwan's avatar
{{ $comment->event->type->name}} <-- check if not null

You laptop works because you have some sample data in database.

kenny11's avatar

You don't have an event related to the certain comment. check your database

tjkalinowski's avatar

mazwan, kenny11

I DID copy production database to laptop development. And two databases are 1 to 1 identical. I did check all tables in Events, Types and Comments table. Tables has XXX_ID fields.

For example :
Comments Table has event_id
Events Table has  type_id
Types Table has name filed.

Then command :

{{ $comment->event->type->name}}

should works.

I did inspect database content and See correct fileds, and its contents. Numbers in event_id or type_id ponts to correct data.

Relation is correctly set up.

Comments.php have
return $this->belongsTo(Event::class);

Event.php have
return $this->belongsTo(Type::class);
return $this->hasMany(Comment::class);

Type.php have
return $this->hasMany(Event::class);

Also I did some experiments in CommentController.php and found that I can acces to object $comment:

//This is experiment:

$comments = Comment::all();
$counts = Comment:all()->count();

//I can access for elements like this:

for ( $i=0; $i<$counts ; $i++) {
echo $comments->event->type->name;
}

But in View:

@foreach($comments as $comment)
 {{$comment->event->type->name}}
@endforeach

///This make error.

I think we have BUG in Laravel core.

But strange that this works:

@foreach($comments as $comment)
 {{$comment->user->name}}
@endforeach

This is double strange , users table is the same defined as one-to-many relationship.

DO you have CLUE?

WHY this works in DEV and NOT in PROD?

Please or to participate in this conversation.