1. <small id='3YDFU'></small><noframes id='3YDFU'>

    <tfoot id='3YDFU'></tfoot>
    <i id='3YDFU'><tr id='3YDFU'><dt id='3YDFU'><q id='3YDFU'><span id='3YDFU'><b id='3YDFU'><form id='3YDFU'><ins id='3YDFU'></ins><ul id='3YDFU'></ul><sub id='3YDFU'></sub></form><legend id='3YDFU'></legend><bdo id='3YDFU'><pre id='3YDFU'><center id='3YDFU'></center></pre></bdo></b><th id='3YDFU'></th></span></q></dt></tr></i><div id='3YDFU'><tfoot id='3YDFU'></tfoot><dl id='3YDFU'><fieldset id='3YDFU'></fieldset></dl></div>

        <bdo id='3YDFU'></bdo><ul id='3YDFU'></ul>
      <legend id='3YDFU'><style id='3YDFU'><dir id='3YDFU'><q id='3YDFU'></q></dir></style></legend>

      Laravel Eloquent 关系的限制结果

      时间:2023-09-24

          <i id='RqrIa'><tr id='RqrIa'><dt id='RqrIa'><q id='RqrIa'><span id='RqrIa'><b id='RqrIa'><form id='RqrIa'><ins id='RqrIa'></ins><ul id='RqrIa'></ul><sub id='RqrIa'></sub></form><legend id='RqrIa'></legend><bdo id='RqrIa'><pre id='RqrIa'><center id='RqrIa'></center></pre></bdo></b><th id='RqrIa'></th></span></q></dt></tr></i><div id='RqrIa'><tfoot id='RqrIa'></tfoot><dl id='RqrIa'><fieldset id='RqrIa'></fieldset></dl></div>

        • <tfoot id='RqrIa'></tfoot>
                <tbody id='RqrIa'></tbody>

              <small id='RqrIa'></small><noframes id='RqrIa'>

              • <bdo id='RqrIa'></bdo><ul id='RqrIa'></ul>

                <legend id='RqrIa'><style id='RqrIa'><dir id='RqrIa'><q id='RqrIa'></q></dir></style></legend>
                本文介绍了Laravel Eloquent 关系的限制结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我有一个简单的相册和图片设置,每个相册都有很多图片.我可以正常获取所有数据,但我想将返回的图像数量限制为 3.我尝试过像这样传递闭包:

                I have a simple set-up of Albums and Images, each album has many images. I can get all the data fine but I want to limit the returned number of images to 3. I have tried passing a closure like so:

                Album::with(['images' =>  function($query) { $query->take(3);}])->get();
                

                这确实将图像数量限制为 3,但将图像总数限制为 3,但我想将每个相册限制为 3 个图像.所以第一个相册会按预期显示 3 张图片,但所有其他相册都没有图片.

                This does limit the number of images to 3 but it limits the total count of images to 3 but I want to limit each album to 3 images. So the first album will show 3 images as expected but all the other albums have no images.

                我尝试向我的模型中添加一个新方法,如下所示:

                I have tried adding a new method to my model like so:

                public function limitImages()
                {
                    return $this->hasMany('AppImage')->limit(3);
                }
                

                我在控制器中调用它:

                Album::with('limitImages')->get();
                

                但这根本不限制返回的图像数量

                But this doesn't limit the image count returned at all

                推荐答案

                我觉得你会很快遇到 N+1 问题,试图实现这一点.只需在它返回的集合中执行此操作:

                I feel you'd quickly run into an N+1 issue trying to accomplish this. Just do it in the collection that it returns:

                Album::with('images')->get()->map(function($album) {
                    $album->setRelation('images', $album->images->take(3));
                    return $album;
                });
                

                这篇关于Laravel Eloquent 关系的限制结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Laravel 原始 SQL 函数的区别 下一篇:在 Views laravel 上使用 carbon

                相关文章

                最新文章

                <small id='SPINj'></small><noframes id='SPINj'>

                <tfoot id='SPINj'></tfoot>
                <legend id='SPINj'><style id='SPINj'><dir id='SPINj'><q id='SPINj'></q></dir></style></legend>

                    <bdo id='SPINj'></bdo><ul id='SPINj'></ul>

                1. <i id='SPINj'><tr id='SPINj'><dt id='SPINj'><q id='SPINj'><span id='SPINj'><b id='SPINj'><form id='SPINj'><ins id='SPINj'></ins><ul id='SPINj'></ul><sub id='SPINj'></sub></form><legend id='SPINj'></legend><bdo id='SPINj'><pre id='SPINj'><center id='SPINj'></center></pre></bdo></b><th id='SPINj'></th></span></q></dt></tr></i><div id='SPINj'><tfoot id='SPINj'></tfoot><dl id='SPINj'><fieldset id='SPINj'></fieldset></dl></div>