laravel - 在索引视图刀片中隐藏克隆帖子
问题描述
我的 livewire 显示刀片中有一个克隆功能,所以当用户克隆帖子时,我不希望克隆帖子显示在一般帖子视图刀片中
Post::get();
查看刀片
@foreach($posts as $post)
{{ $post->title }}
@endforeach
``
(
我不希望在索引中有相同的帖子
我只想在个人资料中显示用户克隆帖子
我该怎么做这个 livewire 组件
public function clone($id)
{
$post = Post::find($id);
$newPost = $post->replicate();
$newPost->created_at = Carbon::now();
$newPost->save();
}
解决方案
您需要一种方法来确定帖子是否被克隆。为此,您可以cloned_post_id
向posts
表中添加一个字段,然后在克隆它时,从原始帖子中设置 ID。
一旦你有了这些数据,你就可以过滤掉你寄存器中的克隆帖子——实现这一点的最好方法是在 Post 模型上添加一个关系,并检查它是否不存在——但你也可以检查whereNull('cloned_post_id')
.
克隆帖子,
public function clone($id)
{
$post = Post::find($id);
$newPost = $post->replicate();
$newPost->cloned_post_id = $post->id;
$newPost->updated_at = Carbon::now();
$newPost->created_at = Carbon::now();
$newPost->save();
}
然后在您的 Post 模型中,添加该关系,
public function clonedPost()
{
$this->belongsTo(Post::class, 'cloned_post_id');
}
然后在您的注册中,检查whereDoesntHave()
,以排除被克隆的帖子,
$posts = Post::whereDoesntHave('clonedPost')->get();
推荐阅读
- python - Python mesa ModularServer错误'int'对象不可调用
- amazon-web-services - Lambda 代码签名:如何获取代码签名?
- spring - Spring MongoDB AggregationOperation 获取文档ID
- node.js - mongodb 中的 find 函数提供了一个数据,需要从中访问一个键和值
- c++ - 如何运行在后台从标准输入读取字符串的 C++ 程序?
- python - 在 tensorflow 中训练 CNN 模型时如何修复 cuDNN 初始化错误?
- fortran - 规范部分中的 Fortran 命令行输入
- c++ - 不能将 PSQL 与 Qt 一起使用
- python - 将python代码转换为python spark代码
- node.js - 写操作后如何使用读操作实现 Firebase 事务