hi @towhid . change this
public function post(){
return $this->hasMany('App\Post');
}
to this
public function posts(){
return $this->hasMany('App\Post');
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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
hi @towhid . change this
public function post(){
return $this->hasMany('App\Post');
}
to this
public function posts(){
return $this->hasMany('App\Post');
}
Hey ! @rin4ik same - Error --
Exception Property [posts] does not exist on this collection instance.
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>
Illuminate \ Database \ Eloquent \ RelationNotFoundException Call to undefined relationship [posts] on model [App\User]. This error ! :(
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
}
Illuminate \ Database \ Eloquent \ RelationNotFoundException Call to undefined relationship [posts] on model [App\User]. same --- :(
ok @towhid all your data should look like this
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
public function posts(){
return $this->hasMany(Post::class);
}
}
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
}
<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>
@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);
}
this is incorrect
$user_id = auth()->user('id');
should be
$user_id = auth()->user()->id;
@rin4ik this is also incorrect:
$user_id = auth()->user();
$user = User::find($user_id);
@tykus it was typo I know
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)
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>
If you dd($user->posts); in the controller, what does it give you?
Can you show your relevant Blade template?
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
$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);
}
i dont know this is right way or wrong ? https://laracasts.com/discuss/channels/laravel/property-posts-does-not-exist-on-this-collection-instance/replies/416545
i think this is not relational Data show - its called again same code use - for direct database :(
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
@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
@rin4ik if i use your code https://laracasts.com/discuss/channels/laravel/property-posts-does-not-exist-on-this-collection-instance/replies/416549 last one which one you suggested then show this error !
Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_PARSE) Parse error: syntax error, unexpected end of file, expecting function (T_FUNCTION) or const (T_CONST)
@towhid. I have given you the answer three times already. I'm out...
@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 ?
<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>
@tykus i already use your code but it show error i tried your suggestion - Dont be Angry ..
please tell me -- last reply https://laracasts.com/discuss/channels/laravel/property-posts-does-not-exist-on-this-collection-instance/replies/416555
this is right way code my scenario or not ?
show your all data. you forgot end of file somewhere
@rin4ik brother i show you again - first code from start please suggest again - if you think i am missing some thing oke..
<?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');
}
}
<?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');
}
}
<?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);
}
}
@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
ErrorException (E_ERROR) Invalid argument supplied for foreach() (View: C:\xampp\htdocs\project1\resources\views\dashboard.blade.php)
@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
just copy this
<?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');
}
}
<?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);
}
}
@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
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.
@iaskakho this thread is 4 years old!
Please or to participate in this conversation.