User Model
Show function public (Post $ POST, $ LogManager log)
{
$ post-> the Load ( "Comments"); // this way is preloaded, if no sentence is just below the model at the time of the query template loaded
return View ( "post.show", Compact ( 'POST'));
}
post model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
//
protected $table = "posts";
public $primaryKey = 'id';
public function user()
{
return $this->belongsTo("App\Models\User","user_id",'id');
}
public function comments()
{
return $this->hasMany('App\Models\Comment','post_id','id')->orderBy("created_at",'desc');
}
}
comment model
<PHP?
namespace the App \ Models;
use Illuminate \ Database \ Eloquent \ the Model;
class it Comment the extends the Model
{
protected Table $ = 'Comments';
public primaryKey $ = 'ID';
public POST function ()
{
return $ this-> a belongsTo ( "the App \ Models \ Post", 'the post_id', 'ID');
}
public function User ()
{
return $ this-> a belongsTo ( "the App \ Models \ the User", 'user_id', 'ID');
}
}
Finally, the paper reviews and comments models want to get the user
to find the object to post
@if(!empty($post->comments))
@foreach($post->comments as $item)
<ul class="list-group" style="padding-bottom:10px;">
<li class="list-group-item">
<h5>{{ $post->created_at }} by {{ $item->user->name }}</h5>
<div>
{{ $item->content }}
</div>
</li>
</ul>
@endforeach
@endif