Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在laravel中获取不同页面的帖子_Laravel_Eloquent - Fatal编程技术网

如何在laravel中获取不同页面的帖子

如何在laravel中获取不同页面的帖子,laravel,eloquent,Laravel,Eloquent,有三个不同的页面和100篇文章,如何获取这三个页面上共享的最新文章?第1页30篇文章(ID->100至71),第2页30篇文章(ID->70至41),第3页40篇文章(ID->40至1)。这可能吗?我只完成了第一部分: $page1 = Post::with('author')->latest()->take(30)->get(); // This gave me exactly 30 latest posts public function scopeLoadThirty(

有三个不同的页面和100篇文章,如何获取这三个页面上共享的最新文章?第1页30篇文章(ID->100至71),第2页30篇文章(ID->70至41),第3页40篇文章(ID->40至1)。这可能吗?我只完成了第一部分:

$page1 = Post::with('author')->latest()->take(30)->get(); // This gave me exactly 30 latest posts
public function scopeLoadThirty($query)
{
 return $query->whereBetween(‘id’, [71, 100]);
}

public function scopeLoadNextThirty($query)
{
 return $query->whereBetween(‘id’, [41, 70]);
}

public function scopeLoadFinal($query)
{
 return $query->whereBetween(‘id’, [1, 40]);
}

此外,我想应用分页,但不知道如何将其与我拥有的内容链接在一起


我非常感谢你的帮助。谢谢。

如评论中所述,您需要的是分页

通常,分页通过获取请求的数量并显示该数量的结果来工作。如果没有该号码,它将显示尽可能多的号码

在您的场景中,它将是30,30,30,10,而不是30,30,40

同样,分页的典型用法是使用单个页面,但在URL中使用
GET
参数来切换页面内容。i、 e

https://mydomain/index?page=1
https://mydomain/index?page=2
https://mydomain/index?page=3
文件中涉及这一点的部分如下:


最后,我想到了一个办法,它按照我的预期工作

我在我的Post模型中声明了如下查询范围:

$page1 = Post::with('author')->latest()->take(30)->get(); // This gave me exactly 30 latest posts
public function scopeLoadThirty($query)
{
 return $query->whereBetween(‘id’, [71, 100]);
}

public function scopeLoadNextThirty($query)
{
 return $query->whereBetween(‘id’, [41, 70]);
}

public function scopeLoadFinal($query)
{
 return $query->whereBetween(‘id’, [1, 40]);
}

然后在我的Post控制器中的索引方法中,我这样查询

use App\Post;

public function indexOne()
{
 $page1 = Post::LoadThirty()->with(‘authors’)->where(‘category’, ‘Catering’)->latest()->paginate(5);

return view(‘posts.indexOne‘, compact(‘page1’));
}

public function indexTwo()
{
 $page2 = Post::LoadNextThirty()->with(‘authors’)->where(‘category’, ‘Engineering’)->latest()->paginate(5);

return view(‘posts.indexTwo’, compact(‘page2’));
}

public function indexThree()
{
 $page3 = Post::LoadFinal()->with(‘authors’)->where(‘category’, ‘Tech’)->latest()->paginate(5);

return view(‘posts.indexThree’, compact(‘page3’));
}


这和我的想法完全一样。

这没用吗<代码>Post::with('author')->latest()->paginate(30)应该做什么itAt@kerb0lz. 谢谢你的帮助,我真的很感激。关于分页的手册很有帮助,但我主要关心的是如何像我在问题中发布的那样在三页上共享100篇文章。如果你想在三页上共享100篇文章,你首先会遇到一个问题:100/3是33.3。所以你要么把它四舍五入到90,每页得到30篇文章,不管你显示多少页,它总是每页30篇文章。我想这可能会有帮助。如果帖子被添加或删除会发生什么?第1页和第2页是否应始终显示30项?如果总共只有29个帖子呢?At@kerbh0lz好问题。它不必是30个项目。由于删除,它可能会更少。如果删除30个项目中的一个项目,laravel会抛出错误吗?这些页面有它们特定的路径。At@Spholt. 谢谢。事实上,分页并不是真正的问题,然而,这是我的错,因为我的问题措辞不恰当。我的目标是为第1页写30篇文章,并将其分页为每个显示5篇文章。第2页和第3页也是如此。基本上,通过ID在三个特定页面上发布100篇文章。希望你能理解。第1页100-71页(按5分页),第2页70-41页(按5分页),第3页40-1页(按5分页)。这正是我的目标。这看起来很奇怪,但同样的原则仍然适用。使用普通的
->where()
类型子句将查询约束到您想要的帖子,然后调用
->paginate(5)
,而不是
->get()
,它的工作原理应该是一样的