I have a comment box in my app and its working fine. but i want to display the user who comment in my post how can i achieve this? i have this code so far.
This is my view
<div class="view-post">
<div class="body">
<h3>{{$posts->title}}</h3>
<h6>{{$posts->created_at->toFormattedDateString()}}</h6>
<p><img src="{{ asset('img/' . $posts->image) }}" class="img-rounded"/></p>
<p>{{$posts->content}}</p>
</div>
<hr>
<section>
<h5>LEAVE US A COMMENT</h5>
<form action="{{ URL::route('createComment', array('id' => $posts->id))}}" method="post">
<div class="form-group">
<input class="form-control" type="text" placeholder="Comment..." name="content">
</div>
<input type="submit" class="btn btn-default" />
</form>
</section><br>
<section class="comments">
@foreach($posts->comment as $comments)
<blockquote>{{$comments->content}}</blockquote>
@endforeach
</section>
</div>
My Controller
public function viewPost($id)
{
$post = Post::find($id);
$user = Auth::user();
$this->layout->content = View::make('interface.viewPost')->with('posts', $post )->with('users',$user);
}
public function createComment($id)
{
$post = Post::find($id);
$comment = new Comment();
$comment->content = nl2br(Input::get('content'));
$post->comment()->save($comment);
return Redirect::route('viewPost', array('id' => $post->id));
}
In your model you can set up relations between one and another model.
Like that..
User Model
class User extends Model {
public function comments()
{
return $this->hasMany('App\Comment');
}
}
Comment Model
class Comment extends Model {
public function user()
{
return $this->belongsTo('App\User');
}
}
So you can get the user with
$comment = Comment::find(1);
$user = $comment->user()->get();