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

towhid's avatar

Property [posts] does not exist on this collection instance.

This is my Dashboard controller code

public function index()
    {
        $user_id = auth()->user('id');
        $user = User::find($user_id);
        return view('dashboard')->with('posts',$user->posts);

         // $user->courses->course_name
    }

this is my Blade -code

<table class="table table-striped">
                        <tr>
                            <th>Title</th>
                            <th></th>
                            <th></th>
                        </tr>
                        <tr>
                            @foreach($posts as $post)
                                <td>{{$post->title}}</td>
                                <td><a href="/posts/{{$post->id}}/edit" class="btn btn-default">Edit</a></td>
                                <td></td>
                            @endforeach
                        </tr>
                    </table>

this is my post model

class Post extends Model
{
    //table name
    protected $table = 'posts';
    // primary key
    public $primaryKey = 'id';
    // Timestamps
    public  $timestamps =true;

    public function user(){
        return $this->belongsTo('App\User');
    }

}

this is my user model

class User extends Authenticatable
{
    use Notifiable;

    protected $fillable = [
        'name', 'email', 'password',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];

    public function post(){
        return $this->hasMany('App\Post');
    }
}

Thank you

0 likes
30 replies
rin4ik's avatar

hi @towhid . change this

public function post(){
        return $this->hasMany('App\Post');
    }

to this

public function posts(){
        return $this->hasMany('App\Post');
    }
1 like
towhid's avatar

Hey ! @rin4ik same - Error --

Exception Property [posts] does not exist on this collection instance.

1 like
rin4ik's avatar

please try this

public function index()
    {
        $user_id = auth()->user('id');
        $user = User::find($user_id);
       $user->load('posts')
 return view('dashboard');

         // $user->courses->course_name
    }
<table class="table table-striped">
                        <tr>
                            <th>Title</th>
                            <th></th>
                            <th></th>
                        </tr>
                        <tr>
                            @foreach($user->posts as $post)
                                <td>{{$post->title}}</td>
                                <td><a href="/posts/{{$post->id}}/edit" class="btn btn-default">Edit</a></td>
                                <td></td>
                            @endforeach
                        </tr>
                    </table>
1 like
towhid's avatar

Illuminate \ Database \ Eloquent \ RelationNotFoundException Call to undefined relationship [posts] on model [App\User]. This error ! :(

1 like
rin4ik's avatar

with this? I updated my reply

public function index()
    {
        $user_id = auth()->user('id');
        $user = User::find($user_id);
       $user->load('posts')
 return view('dashboard')->with('user',$user);


         // $user->courses->course_name
    }
1 like
towhid's avatar

Illuminate \ Database \ Eloquent \ RelationNotFoundException Call to undefined relationship [posts] on model [App\User]. same --- :(

1 like
rin4ik's avatar

ok @towhid all your data should look like this

User model

class User extends Authenticatable
{
    use Notifiable;

    protected $fillable = [
        'name', 'email', 'password',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];

    public function posts(){
        return $this->hasMany(Post::class);
    }
}

Controller

public function index()
    {
        $user_id = auth()->user()->id;
        $user = User::find($user_id);
       $user->load('posts')
 return view('dashboard')->with('user',$user);


         // $user->courses->course_name
    }

View

<table class="table table-striped">
                        <tr>
                            <th>Title</th>
                            <th></th>
                            <th></th>
                        </tr>
                        <tr>
                            @foreach($user->posts as $post)
                                <td>{{$post->title}}</td>
                                <td><a href="/posts/{{$post->id}}/edit" class="btn btn-default">Edit</a></td>
                                <td></td>
                            @endforeach
                        </tr>
                    </table>
1 like
tykus's avatar

@towhid I suspect you are not getting a User instance as the logic you are using to get the user is incorrect.

This $user_id = auth()->user('id'); will return a User instance, not an id, so this $user = User::find($user_id); will return a Collection, not a User instance. Then, this $user->posts is attempting to get a posts property on a Collection instance.

You can reduce the index method to:

public function index()
{
        $user = auth()->user();
        return view('dashboard')->with('posts', $user->posts);
}
rin4ik's avatar

this is incorrect

     $user_id = auth()->user('id');

should be

$user_id = auth()->user()->id;
1 like
tykus's avatar

@rin4ik this is also incorrect:

        $user_id = auth()->user();
        $user = User::find($user_id);
1 like
towhid's avatar

when iam trying to do this error show

$user = auth()->user();
        return view('dashboard')->with('posts', $user->posts);
Invalid argument supplied for foreach() (View: C:\xampp\htdocs\project1\resources\views\dashboard.blade.php)
1 like
rin4ik's avatar

now u have posts not user->posts. change your blade to this

<table class="table table-striped">
                        <tr>
                            <th>Title</th>
                            <th></th>
                            <th></th>
                        </tr>
                        <tr>
                            @foreach($posts as $post)
                                <td>{{$post->title}}</td>
                                <td><a href="/posts/{{$post->id}}/edit" class="btn btn-default">Edit</a></td>
                                <td></td>
                            @endforeach
                        </tr>
                    </table>
1 like
tykus's avatar

If you dd($user->posts); in the controller, what does it give you?

Can you show your relevant Blade template?

towhid's avatar

if i use this code for post model

 public function user(){
        return $this->belongsTo('App\User');
    }

code for user model

    public function post(){
        return $this->hasMany('App\Post');
    }

this is controller code


public function index()
    {
        $posts = Post::all();
        $user_id = auth()->user()->id;
        $user = User::find('user_id'); //->with('posts',$user->posts)
        return view('dashboard')->with('user',$user)->with('posts',$posts);
}

this is blade code

@foreach($posts as $post)
                            <tr>
                                <td>a{{$post->title}}</td>
                                <td><a href="/posts/{{$post->id}}/edit" class="btn btn-default">Edit</a></td>
                            </tr>
                            @endforeach

an out put is title with Edit action blade view

tykus's avatar

$user->posts is returning NULL because the relation is still called post - you must update the relation name:

    public function posts(){
        return $this->hasMany('App\Post');
    }

I already told you how to simplify your controller index method; but, you're changing the requirements. Post::all() will return _every` post whether it belongs to the authenticated user or not!


public function index()
{
        $user = auth()->user();
        return view('dashboard')->with('user', $user)->with('posts', $user->posts);
}
rin4ik's avatar

please stick with this

public function posts(){
        return $this->hasMany('App\Post');
    }
public function index()
    { 
        $user_id = auth()->user()->id;
        $user = User::find('user_id');  
    $user->load('posts');
        return view('dashboard')->with('user',$user);
}
@foreach($user->posts as $post)
                            <tr>
                                <td>a{{$post->title}}</td>
                                <td><a href="/posts/{{$post->id}}/edit" class="btn btn-default">Edit</a></td>
                            </tr>
                            @endforeach
rin4ik's avatar

@towhid what u want . please elaborate your problem. u want posts from user? or all posts? just copy paste code above and you're good to go

tykus's avatar

@towhid. I have given you the answer three times already. I'm out...

towhid's avatar

@rin4ik brother i need to out output from relational table Scenario is : my posts table has one column name is user id , now i just want to show post from this user id which one logged in , and those post show which post created this Admin id - , i hope you understand my problem -,

if i can i will show the out put which one i want to view my application . any option to show here any image screen shot ?

rin4ik's avatar

@towhid

view

<table class="table table-striped">
                        <tr>
                            <th>Title</th>
                            <th></th>
                            <th></th>
                        </tr>
                        <tr>
                            @foreach($user->posts as $post)
                                <td>{{$post->title}}</td>
                                <td><a href="/posts/{{$post->id}}/edit" class="btn btn-default">Edit</a></td>
                                <td></td>
                            @endforeach
                        </tr>
                    </table>
rin4ik's avatar

show your all data. you forgot end of file somewhere

towhid's avatar

@rin4ik brother i show you again - first code from start please suggest again - if you think i am missing some thing oke..

post model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    //table name
    protected $table = 'posts';
    // primary key
    public $primaryKey = 'id';
    // Timestamps
    public  $timestamps =true;

    public function user(){
        return $this->belongsTo('App\User');
    }

}

user model

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    protected $fillable = [
        'name', 'email', 'password',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];

    public function post(){
        return $this->hasMany('App\Post');
    }
}

controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;

class DashboardController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        $user_id = auth()->user('id');
        $user = User::find($user_id);
        return view('dashboard')->with('posts',$user->posts);
    }
}

view

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Dashboard</div>

                <div class="card-body">
                    @if (session('status'))
                        <div class="alert alert-success">
                            {{ session('status') }}
                        </div>
                    @endif
                    <a href="/posts/create" class="btn btn-primary">create post</a>
                    <h2>Your Blog Posts</h2>
                    <table class="table table-striped">
                        <tr>
                            <th>Title</th>
                            <th></th>
                            <th></th>
                        </tr>
                        
                            @foreach($posts as $post)
                            <tr>
                                <td>{{$post->title}}</td>
                                <td><a href="/posts/{{$post->id}}/edit" class="btn bt">Edit</a></td>
                            </tr>
                            @endforeach
                        
                    </table>

                </div>
            </div>
        </div>
    </div>
</div>
@endsection

output is

ErrorException (E_ERROR) Invalid argument supplied for foreach() (View: C:\xampp\htdocs\project1\resources\views\dashboard.blade.php)

rin4ik's avatar
rin4ik
Best Answer
Level 50

@towhid please change this

  public function post(){
        return $this->hasMany('App\Post');
    }

to this

  public function posts(){
        return $this->hasMany('App\Post');
    }

u call $user->posts but in your model you specified post

1 like
rin4ik's avatar

just copy this

user model

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    protected $fillable = [
        'name', 'email', 'password',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];

    public function posts(){
        return $this->hasMany('App\Post');
    }
}

controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;

class DashboardController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        $user_id = auth()->user()->id;
        $user = User::find($user_id);
        return view('dashboard')->with('posts',$user->posts);
    }
}

view

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Dashboard</div>

                <div class="card-body">
                    @if (session('status'))
                        <div class="alert alert-success">
                            {{ session('status') }}
                        </div>
                    @endif
                    <a href="/posts/create" class="btn btn-primary">create post</a>
                    <h2>Your Blog Posts</h2>
                    <table class="table table-striped">
                        <tr>
                            <th>Title</th>
                            <th></th>
                            <th></th>
                        </tr>
                        
                            @foreach($posts as $post)
                            <tr>
                                <td>{{$post->title}}</td>
                                <td><a href="/posts/{{$post->id}}/edit" class="btn bt">Edit</a></td>
                            </tr>
                            @endforeach
                        
                    </table>

                </div>
            </div>
        </div>
    </div>
</div>
@endsection
1 like
iaskakho's avatar

The issue is that you are trying to pull the relationship from a collection of items and not a single instance.

$user = User::find($user_id); - FIND returns a collection, thus you need ->first()->posts.

Query a single instance and then ask for the relationship. A collection doesn't have relationships, its a list of items.

1 like

Please or to participate in this conversation.