Property [user] does not exist on this collection instance

I have found the error Property [user] does not exist on this collection instance because I was trying to get the data of the user table using user relationship method on the post model with multiple records without iterating foreach loop. If you have multiple records in collection then you can not use $post->user code without foreach.

Answers 1
  • Get user table data with posts using belongsTo method

    $posts = Post::with('user')->get();
    foreach($posts as $post){
        echo $post->user;
    }
    
    0

    This code snippet will help you to get the user/author data with posts on multiple records. You have multiple post and you want to access the user data then you have to iterate data using foreach to get the user table data.

  • Back to code snippet queries related laravel