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

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

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

        <tfoot id='k0jd2'></tfoot>

        <legend id='k0jd2'><style id='k0jd2'><dir id='k0jd2'><q id='k0jd2'></q></dir></style></legend>

        Laravel/Eloquent - 急切加载隐藏/可见属性

        时间:2023-09-24
            1. <tfoot id='3FznE'></tfoot>
            2. <legend id='3FznE'><style id='3FznE'><dir id='3FznE'><q id='3FznE'></q></dir></style></legend>

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

                  <bdo id='3FznE'></bdo><ul id='3FznE'></ul>

                  <small id='3FznE'></small><noframes id='3FznE'>

                    <tbody id='3FznE'></tbody>
                • 本文介绍了Laravel/Eloquent - 急切加载隐藏/可见属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  在使用 Laravel 的 Eloquent ORM 时,我似乎无法在模型上动态设置 $hidden 和 $visible 属性.

                  When using Laravel's Eloquent ORM, I can't seem to set the $hidden and $visible properties on my Model dynamically.

                  示例 1:这有效:

                  class User extends Eloquent {
                     $this->visible = array('field_name');
                  
                     function read() 
                     {
                        return User::all();
                     }
                  }
                  

                  示例 2:动态设置 Eloquent 类的可见属性,不起作用:

                  class User extends Eloquent {
                     function read($visible = array('field_name'))
                     {
                        $this->visible = $visible; // Also tried: $this->setVisible($visible);
                  
                        return User::all();
                     }
                  }
                  

                  示例 3:适用于模型本身的解决方案,但不适用于急切加载的模型:

                  class User extends Eloquent {
                     function read($visible = array('field_name'))
                     {
                        $users = User::all();
                  
                        return $users->get()->each(function($row) use ($visible) {
                           $row->setVisible($visible);
                        });
                     }
                  }
                  

                  为了在 Eagerly Loaded Models 上动态设置 $visible 属性,除了让示例 2 工作之外,我没有看到其他解决方案.但是如何?

                  In order to set the $visible property dynamically on Eagerly Loaded Models, I don't see another solution than to get Example 2 to work. But how?

                  推荐答案

                  由于 $visible 是在实例级别设置的(即它不是在同一类型的所有模型之间共享的静态变量),不 - 没有更好的方法可以做到这一点.

                  As $visible is set on an instance level (i.e. it's not a static variable shared between all models of the same type), no - there's no better way to do this.

                  这篇关于Laravel/Eloquent - 急切加载隐藏/可见属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Laravel Eloquent orWhere 查询 下一篇:Eloquent - Eager 加载关系

                  相关文章

                  最新文章

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

                      <tfoot id='rTplH'></tfoot>
                      • <bdo id='rTplH'></bdo><ul id='rTplH'></ul>
                      <legend id='rTplH'><style id='rTplH'><dir id='rTplH'><q id='rTplH'></q></dir></style></legend>

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