
Laravel withcount的定义
Laravel有一个独特的函数叫做withCount(), 它是用来获取主对象中的相关记录的数量。它也隐含在单层和多层,并与里面的许多关系一起工作。当用户需要计算关系模型中的行数时, 他可以直接运行withCount().laravel withcount的模型是通过许多关系层来建立的。在这篇文章中, 使用laravel withcount, 实现, 配置, 和一些Laravel count的标准例子都被简单的讨论。
laravel withcount的概述
简单来说, Laravel withCount()是用来计算关系模型中的行数,并获取主对象中的关联或相关记录的数量。它可以实现许多关系,如一对一,一对多,多对多,多态关系,查询关系,动态关系,等等。
为什么使用laravel withcount?
每个关系中都有多个过程存在,每个过程都有独特的作用。例如, 在下面的代码中, 有三个模型, 评论, 帖子, 和用户.这里使用的所有关系都应该在app/ user.php模型文件中预先定义好。
简单计数的例子被定义在下面的代码中。
$category = Categories :: withCount ('document') -> get(); //count of documents present in categories.
多重计数的例子定义在下面的代码中。
$category = Categories :: withCount ('document', ‘files’) -> get(); //count of documents and files present in categories.
下面给出了withCount()的条件例子。
$category = Categories :: withCount ('document' = function ($query) ) { $query -> where (‘status’, 1); }]} -> get ();
//显示类别中存在的文件数。
public function comment () { return $this -> has Many (comment::user); } public function post() { return $this -> hasManyThrough (post ::user, comment::user);
为了显示用户,帖子和饲料的列表,有一个方法,暗示用户控制器的代码,这是定义。
public function value () { $user = User :: withCount (['posts', ‘feeds’]) -> get (); return view('user', compact('user')); }
如何使用laravel withCount()?
在指定的withCount()方法中定义的值变成了主对象的计数。所以在这里,用户必须定义user -> feeds的总计数。都被定义为变量。计数相关模型可以使用户在不喂食模型的情况下为所提供的关系计数相关模型。为了实现这一点,用户可以选择withCount()技术。withCount()技术提供了{relation_count}和结果模型上的属性。
Use app\ feeds\ post; $ post = Post:: withCount ('comments') -> get(); For each ($post $feeds $post) { echo $post->comments_count; }
当它以数组形式传递给withCount()方法时,用户可以在多个关系中添加计数,也可以在开发的查询中添加额外的约束。
use Illuminate\ Database\ Eloquent\ Build; $post = Post :: withCount (['post', ‘feeds’ => function (build $query) { $query -> where ('content', 'feed', 'code%'); }]) -> get(); Echo $posts[0] -> Post_count; echo $posts[0] – > feeds_count;
用户可以别名关系的结果计数,这使得用户可以在同一关系中的多个计数上工作。
$post = Post:: withCount ([ ‘feeds’, feeds are ttotal pending counts in the pos' => function (Build $query) { $query- > where('approved', true); }, ]) -> get(); echo $posts[0] -> Post_count; echo $posts[0] -> total pending counts in the post;
在计数的延迟加载中,用户在检索父模型后加载关系的计数。
$comic = comic :: first(); $comic -> loadCount ('genres');
如果用户必须对总的查询次数进行固定,那么他就可以通过用户需要进行相应计数的关系来传递数组键。数组的值应该是封闭的,通过构建器选项接收实例。
$comic -> loadCount (['pages' => function ($query) { $query->where ('price', 500); }])
Laravel withcount例子
定制的选择语句和关系计数可以与withCount()语句相结合,并在选择方法后暗示。
$comic = Post:: select(['title', 'body']) -> withCount (‘feeds’) -> get ();
除了withCount()方法, laravel还提供了withmax, withmin, withsum, withavg, 和 withexist方法.它在结果模型上替换了一个关系_函数_行属性
$comics = comic :: withSum(‘pages’, 'price’) -> get(); foreach ($feeds as $post) { echo $comic ->price _ sum _ pages; }
用户也可以给聚合函数的结果起另一个名字, 而且应该在一般的别名中提到。
$comics = comics:: withSum (‘price _ sum _ pages’ as ‘price’, ‘pages’) -> get(); foreach ($($feeds as $post); { echo $comic ->price _ sum _ pages; }
类似于加载计数方法,延迟版本是可能的,额外的操作在已经可以被检索的模型中执行。
$comic = comic :: first(); $comic -> loadSum (‘pages’, 'price');
如果用户在withCount()中同时使用选择和聚合语句,他应该检查他是否可以在选择方法之后才调用所有聚合方法。
$comic = Post:: select(['title', 'body']) -> withexceeds (‘feeds’) -> get ();
在laravel中,通过使用关系和withCount()也可以实现计数相关的模型。如果用户必须在关系中加载一个形态,他可以将不同实体的模型计数联系起来,这是由关系返回的。它可以使用withCount()方法在morph与morphwithcount方法的组合关系中得到利用。
结论
在上面给出的例子中,漫画和帖子与创建feed模型有很大关系。因此,活动Feed有一个叫做parent的morph to关系,它能够获取活动Feed上的实例的帖子数或feed数。帖子有许多标签和feeds或评论,但所有的数据都可以单独检索到。
推荐文章
这是一个关于Laravel withcount的指南。在这里,我们讨论了定义,概述,为什么使用laravel withcount,例子与代码实现。你也可以看看下面的文章来了解更多 -
The postLaravel withcountappeared first onEDUCBA.