Trying to get property ‘uid’ of non-object

  belongs-to, has-one, laravel, laravel-blade, php

In blade I’m trying to determine which button needs to show, but I get the following error:
Trying to get property ‘uid’ of non-object.

In my Entry model I made this relation:

public function blockeduser() {
    return $this->hasOne(BlockedUser::class);
}

In my BlockedUser model I made this relation:

public function entry() {
    return $this->belongsTo(Entry::class);
}

The code in the blade file:

@foreach($entries as $entry)
<tr>
    <td style="max-width: 150px;">
        <div class="btn-group">
            <form target="_blank" method="post" action="{{route('entries.show', $entry->id)}}">
                @method("GET")
                <button style="margin-right: 5px;" type="submit" class="btn btn-success">View</button>
            </form>

            @if ($entry->uid !== $entry->blockeduser->uid)
                <form method="post" action="{{route('entries.blockUser', $entry->id)}}">
                    @csrf
                    <button style="margin-right: 5px;" type="submit" class="btn btn-warning">Block</button>
                </form>

            @else
                <form method="post" action="{{route('entries.unblockUser', $entry->id)}}">
                    @csrf
                    <button style="margin-right: 5px;" type="submit" class="btn btn-warning">Unblock</button>
                </form>
            @endif

            <form method="post" action="{{route('entries.destroy', $entry->id)}}">
                @csrf
                @method("DELETE")

                <button style="margin-right: 5px;" type="submit" class="btn btn-danger">Delete</button>
            </form>
        </div>
    </td>
</tr>
@endforeach

How can I access the uid inside the blocked users table that has a relation with the Entry and compare them and determine which form needs te show.

Or is there a way I can check if a table contains a row that has a relation with the Entry?

Source: Laravel

Leave a Reply