【问题标题】:Laravel display latest comment of a postLaravel 显示帖子的最新评论
【发布时间】:2021-05-14 13:18:03
【问题描述】:

我有一个话题表,我想在索引页面上显示该话题的最新评论。到目前为止我已经选择了最新的评论并尝试在页面上显示它,但是它只显示整体的最新评论,而不是特定帖子的最新评论

所以我的 ThreadsController 现在看起来像这样,选择所有 cmets 并首先显示最新的。

 public function index()
    {
        $threads = Thread::latest()->paginate(10);
        $latestComment = Comment::latest()->first();
        return view('threads.index', compact('threads', 'latestComment'));
    }

线程模型

public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function comments()
    {
        return $this->morphMany(Comment::class, 'commentable');
    }

评论模型

public function user() {
        return $this->belongsTo(User::class);
    }

    public function thread()
    {
        return $this->belongsTo(Thread::class);
    }

    public function commentable() {
        return $this->morphTo();
    }

那么如何从特定线程中选择最新的评论并将其显示在索引上?

编辑:

控制器:

public function index()
    {
        $threads = Thread::latest()->with('comments')->paginate(10);

        return view('threads.index', compact('threads'));
    }

索引刀片:

@foreach($threads as $thread)
    {{ $thread->latestComment->user->name }}
@endforeach

评论表迁移

 public function up()
    {
        Schema::create('comments', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('user_id');
            $table->string('body');

            $table->unsignedBigInteger('commentable_id');
            $table->string('commentable_type');
            $table->timestamps();
        });
    }

【问题讨论】:

  • 您需要在ThreadComments 之间创建一个relationship
  • @Unflux 是的,他们在我的模型中有关系一直运行良好,但这个问题我真的不知道如何解决
  • 你能提供显示你们关系的代码吗?

标签: laravel controller forum


【解决方案1】:

您已经正确定义了ThreadComment 之间的关系,但您没有在ThreadsControllerindex 函数中使用该关系。您只是抓取了最新的 10 个Threads 和最新的Comment,而不是与您的Thread 相关的评论。

您想要的是获取您所有的Threads,并使用最新的Comment 将它们传递给您的视图。

您可以使用eager loading 将您的Comment 关系附加到您的模型。

Thread::latest()->with('comments')->paginate(10);

这将获取您最新的 10 个Threads 以及他们的Comments。因此,在您看来,您可以执行以下操作:

@foreach ($threads as $thread)
    {{ $thread->comments()->latest('id')->first()->comment }}
@endforeach

虽然这可行,但它有点冗长。所以你可以做的是背负你的comments函数,只返回最近的Comment作为关系。

class Thread extends Model
{
    use HasFactory;

    public function comments()
    {
        return $this->morphMany(Comment::class, 'commentable');
    }

    public function latestComment()
    {
        return $this->morphOne(Comment::class, 'commentable')->latest('id');
    }
}

这为您提供了更短的方法来访问最新的Comment 以获取Thread

所以回到你的场景;

在你的ThreadsController.php

public function index()
{
  $threads = Thread::latest()->with('latestComment')->paginate(10);
  return view('threads.index', compact('threads'));
}

然后在您的index.blade.php 文件中

@foreach ($threads as $thread)
  {{-- Access whatever properties you have on your comment --}}
  {{ $thread->latestComment->id }} 
@endforeach

【讨论】:

  • 我不太明白。您能用新信息更新您的问题吗?
  • 更新了图片,基本上没有什么限制它只显示一条评论,它仍然显示所有的 cmets,而不是与该特定线程关联的 cmets
  • 能否提供更新后的index 方法和blade view 代码?
  • @OskarB 确保您的Thread 模型上具有latestComments 关系,并在您的控制器中将with('comments') 替换为with('latestComment')
  • 那是一个错误,当我尝试了你写的两个版本时忘记改回来。当我使用正确的 with() 时,输出仍然相同
【解决方案2】:
Comment::all()->orderBy('id', 'DESC')->get()

【讨论】:

  • 那仍然只会输出所有的 cmets
  • 试试这个:Comment::all()->orderBy('id', 'DESC')->get()->first()
【解决方案3】:

您可以在胎面和注释之间创建关系。 然后在刀片中你可以做这样的事情: $thread->cmets->latest()->first()

【讨论】:

  • 我不能在刀片页面中使用最新的方法,我认为它必须在控制器中完成
  • 然后你可以直接在你的控制器中格式化你的数据,并准确返回你需要的数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 2016-11-07
  • 2019-08-24
  • 1970-01-01
  • 2022-01-10
  • 2017-07-29
相关资源
最近更新 更多