I'm trying to get user name from user_id. I was not able to get the name. i have only store-action, didn't create show action.
Error massage: Trying to get property 'name' of non-object (View: /var/www/html/laravel-app/resources/views/bbs/show.blade.php)
CommentsController.php
public function store(CommentRequest $request)
{
$comment = new Comment;
$comment->comment = $request->comment;
$comment->name = "";
$comment->post_id = $request->post_id;
$comment->save();
return redirect()->route('bbs.show', [$comment['post_id']])->with('commentstatus','');
}
show.blade.php
@forelse($post->comments as $comment)
<div class="border-top p-4">
<time class="text-secondary">
{{ optional ($comment)->user->name }} /
{{ optional ($comment->created_at)->format('Y.m.d H:i') }} /
{{ optional ($comment)->id }}
</time>
Comment.php
/**
*
*/
public function post()
{
return $this->belongsTo('App\Post');
}
/**
*
*/
public function user()
{
return $this->belongsTo('App\User');
}