I would like to display the name of the user who posted the comment.
Comments can be displayed, but the name of the contributor cannot be displayed.
Try changing the migration file method and type
I looked up articles about the comment function and related articles, but I couldn't find them.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
useApp\Http\Requests\CommentRequest;
use App\Comment;
use Auth;
class CommentsController extensions Controller
{
public function store (CommentRequest$request)
{
$comment = new comment;
$comment->comment=$request->comment;
$comment->user_id=Auth::id();
$comment->name=";
$comment->post_id=$request->post_id;
$comment->save();
return redirect()->route('bbs.show', [$comment['post_id']])->with('commentstatus', 'comment posted');
}
public function post()
{
return$this->belongsTo('App\Post');
}
public function user()
{
return$this->belongsTo('App\User');
}
public function up()
{
Schema::create('comments', function(Blueprint$table){
$table->bigIncrements('id');
$table->timestamps();
$table->string('is_deleted',4)->default('0');
$table->integer('post_id');
$table->string('name');
$table->text('comment');
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
});
}
@forelse($post->comments as $comment)
<div class="border-top p-4">
<time class="text-secondary">
{{ $comment->user->name}}/ Where to display the name
{{ optional($comment->created_at)->format('Y.m.d H:i')}}/
{{ optional($comment)->id}}
</time>
The user_id column is required in the comments table.
© 2023 OneMinuteCode. All rights reserved.