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

jeroenvanrensen's avatar

Laravel Vue - v-bind only works if logged in

Hi everyone,

I'm creating a forum with Laravel and I've got the following problem:

If I'm logged in in the browser, it totally works. But if I go to a private window, I got the following message:

Method Illuminate\Database\Eloquent\Collection::__toString() must not throw an exception, caught ErrorException: Trying to get property 'id' of non-object 

Does anyone know what to do?

Thank you! Jeroen

If you need more information, please ask me.

Here's the resources\views\threads\show.blade.php file, where I'm calling the replies component:

<replies :data="{{ $replies }}" @removed="repliesCount--"></replies>

And here's the Replies.vue component:

<template>
    <div>
        <new-reply @created="add"></new-reply>

        <div v-for="(reply, index) in items">
            <reply :data="reply" @deleted="remove(index)"></reply>
        </div>
    </div>
</template>

<script>
    import Reply from './Reply.vue';
    import NewReply from './NewReply.vue';

    export default {
        props: [
            'data'
        ],
        
        components: { Reply, NewReply },

        data() {
            return {
                items: this.data
            }
        },

        methods: {
            add(reply) {
                this.items.push(reply);
            },

            remove(index) {
                this.items.splice(index, 1);

                this.$emit('removed');

                toast('Your reply has been deleted!');
            }
        }
    }
</script>
0 likes
3 replies
m7vm7v's avatar

What do you have in your controller?

I'd assume that you have a variable that you are requesting ->id but the variable is null. By the information provided I think you are requesting an $user->id but you are not logged in.

  1. Add auth middleware for the route Route::get('/...', 'TheController@method')->middleware('auth')
  2. Use optional($user)->id //recommended or $userId = $user ? $user->id : false

Amend the logic, but it be more helpful is you post the controller executing that view

jeroenvanrensen's avatar

Hi @m7vm7v,

This is my show function in my ThreadController:

public function show($category, Thread $thread)
{
    return view('threads.show', [
        'thread' => $thread,
        'replies' => $thread->replies()->oldest()->get()
    ]);
}

I don't think it has something to do with this, but I'm not sure.

jeroenvanrensen's avatar
Level 15

Hi everyone,

Suddenly my problem was solved! I don't know why or how but the problem is over!

Jeroen

Please or to participate in this conversation.