The error is like below:
Trying to get property of non-object (View: C:\xampp\htdocs\laravel\app\views\singlePost.blade.php)
There is 2 tables: Comments and Users. In comments table there is a column named user_id and that refers to id column that is in users table. And there is username column in the users table. This is how I try to print username.
@foreach($theComments as $theComment)
<div>{{$theComment->users->username}}</div>
<div style="border:1px solid black;margin:8px 0px 8px 0px">{{$theComment['content']}}</div>
@endforeach
and controller:
public function singlePost(Posts $post)
{
$id = $post['id'];
$comments = Comments::where('post_id','=',$id)->get();
$users = Users::all();
return View::make('singlePost')->with('thePost', $post)->with('theComments', $comments)->with('theUser', $users);
}
and /Model/Comments.php
<?php
class Comments extends Eloquent{
protected $fillable = array('user_id');
public function users(){
return $this->belongsTo('Users');
}
}
What's the problem and how can i solve it?