如何将帖子从两个不同来源传递到同一刀片视图但按日期排序
标题看起来有点复杂,其实很简单。 我有一个带有表格和所有内容的 Post 模型,在 PostController 中我将帖子传递到视图,如下
所示在我的控制器中,我从 API 获取我的文章并将其传递给 $articles 变量,我从以下位置获取我的帖子数据库,我将其传递给 $posts 变量,
public function index()
{
$articles = Http::withHeaders([
'api-key' => config('services.devto.key'),
])->get('http://dev.to/api/articles/me/published');
$articles = json_decode($articles, true);
return view('posts.index', [
'posts' => Post::latest()->filter(request(['search', 'category', 'author']))->paginate(6)->withQueryString(),
'articles' => $articles
]);
}
我可以将两者传递给同一个视图并执行如下操作,效果很好
<div class="lg:grid lg:grid-cols-3">
@foreach ($posts->skip($posts->onFirstPage() ? 3 : 0) as $post)
<x-postCard :post="$post" />
@endforeach
@foreach ($articles as $article)
<x-articlepostcard :article="$article" />
@endforeach
</div>
这给了我 6 个帖子,因为我对它们进行了分页,然后在我从 API 中获取所有 10 篇文章之后,然后在第 2 页中,我得到了另外 6 个帖子,在它们下面再次重复了相同的 10 篇文章,因为我无法分页它们只是变量中的 json 。
关于如何使我的视图显示帖子和文章完全分页并按日期排序有什么想法吗?
Headline seems a bit complicated but It's simple.
I have a Post model with table and everything, in the PostController I'm passing posts to the view as below
Here in my controller I'm getting my articles from API and passing it to $articles variable and I'm getting my posts from database and I'm passing it to $posts variable
public function index()
{
$articles = Http::withHeaders([
'api-key' => config('services.devto.key'),
])->get('http://dev.to/api/articles/me/published');
$articles = json_decode($articles, true);
return view('posts.index', [
'posts' => Post::latest()->filter(request(['search', 'category', 'author']))->paginate(6)->withQueryString(),
'articles' => $articles
]);
}
I can pass both to the same view and do as below and it works fine
<div class="lg:grid lg:grid-cols-3">
@foreach ($posts->skip($posts->onFirstPage() ? 3 : 0) as $post)
<x-postCard :post="$post" />
@endforeach
@foreach ($articles as $article)
<x-articlepostcard :article="$article" />
@endforeach
</div>
This gives me 6 of my posts because i paginate them and then right after i get all 10 articles from my API and then in page 2 i get another 6 of my posts and below them the same 10 articles repeated again because i can't paginate them as they're just json in a variable.
Any ideas on how to make my view show posts and articles altogether paginated and sorted by date?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建一个代表帖子或文章的数据传输对象。
例如(假设您使用的是 PHP 8):
然后,您获取您的文章和帖子,并将它们实例化到 DTO 中并从中创建一个集合,
然后您可以传递集合,按日期排序,供您查看。
此解决方案的注意事项
为了保持分页正常工作,您可能需要手动调用分页器,但您仍然会在每个请求上加载所有数据。
You can create a data transfer object that will represent a post or an article.
For instance (assuming you're using PHP 8):
Then, you fetch your articles and your posts, and you instantiate each of them into the DTO and make a collection out of it
You can then pass the collection, sorted by date, to your view.
Caveat to this solution
In order to keep the pagination working, you might need to manually invoke the paginator but then you'll still load all the data on each request.