Categories
Development PHP

how to display the username of the comment in the post with relation table

my relational database image


if I use the code above

name user show in all post

my model

    <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Komen extends Model
{
    protected $table='komentar_post';


}

my controller

public function index()
{
    $post=Post::all();
    $comment=Komen::all();
    $user=User::all();

    // dd($id);
    return view('home',compact('post','comment','user'));
}

my view

@foreach($comments as $cm)
                
                @endforeach

what the corect query i must use

Leave a Reply

Your email address will not be published. Required fields are marked *