Show the name of the commented user

Asked 2 years ago, Updated 2 years ago, 48 views

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.

CommentsController.php

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');
    }

Comment.php

public function post()
    {
        return$this->belongsTo('App\Post');
    }

    public function user()
    {
        return$this->belongsTo('App\User');
    }

comments_table.php

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');
        });
    }

show.blade.php

@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>

php laravel

2022-09-30 11:09

1 Answers

The user_id column is required in the comments table.


2022-09-30 11:09

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.