• <tfoot id='DTG6n'></tfoot>

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

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

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

      1. <i id='DTG6n'><tr id='DTG6n'><dt id='DTG6n'><q id='DTG6n'><span id='DTG6n'><b id='DTG6n'><form id='DTG6n'><ins id='DTG6n'></ins><ul id='DTG6n'></ul><sub id='DTG6n'></sub></form><legend id='DTG6n'></legend><bdo id='DTG6n'><pre id='DTG6n'><center id='DTG6n'></center></pre></bdo></b><th id='DTG6n'></th></span></q></dt></tr></i><div id='DTG6n'><tfoot id='DTG6n'></tfoot><dl id='DTG6n'><fieldset id='DTG6n'></fieldset></dl></div>
      2. 如何在使用 eloquent 时排除某些列

        时间:2023-09-24
          • <tfoot id='HciZB'></tfoot>

              <tbody id='HciZB'></tbody>

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

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

                  本文介绍了如何在使用 eloquent 时排除某些列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  当我使用 eloquent 时,我可以使用where"方法,然后使用get"方法来填充包含我在数据库中选择的对象的对象.我的意思是:

                  When I'm using eloquent, I can use the "where" method then the method 'get' to fill an object containing what I've selected in my database. I mean:

                  $users = User::where('gender', 'M')->where('is_active', 1)->get(['pseudo', 'email', 'age', 'created_at'])->toArray();
                  

                  在这里我可以选择我想要的列,如伪"、电子邮件"等.但是我在 laravel doc 中想念的是相反的方法.可能是这样的:

                  Here I can choose the columns I want to get like 'pseudo', 'email', etc.. But what I miss in laravel doc is the way to do the contrary. It could be something like that:

                  $users = User::where('gender', 'M')->where('is_active', 1)->notGet(['pseudo', 'email', 'age', 'created_at'])->toArray();
                  

                  感谢您的回答,祝您有美好的一天.

                  Thank you for you futur answer and have a nice day.

                  推荐答案

                  如果您只需要从模型的数组或 JSON 表示中隐藏属性,您可以使用一种或两种方法:

                  If you only need to hide attributes from your model's array or JSON representation, you may use one or both approaches:

                  • 添加$hidden 属性给你的模型
                  • Add the $hidden property to your model
                  class User extends Model
                  {
                      /**
                       * The attributes that should be hidden for arrays.
                       */
                       protected $hidden = ['password'];
                  }
                  

                1. 使用makeHidden功能

                  $users = $users->makeHidden(['address', 'phone_number']);
                  

                2. 有关更多详细信息,请参阅其他答案... 但是 有时您不想将大量数据(地理空间、html、日志...)加载到您的应用程序中,它会很慢并且需要更多的记忆.OP 要求进行 SQL 查询,因此我的回答是,但大多数情况下,仅从 JSON 响应中隐藏数据会更方便.

                  See other answers for more details... But sometimes you don't want to load huge data (geospatial, html, logs...) into your application, it will be slow and take more memory. OP asked for an SQL query hence my answer, but most of the time it's more convenient to only hide the data from the JSON response.

                  AFAIK SQL 中没有内置选项来显式排除列,所以 Laravel 不能这样做.但是你可以试试这个技巧

                  AFAIK there is no build in option in SQL to exclude columns explicitly, so Laravel can't do it. But you can try this trick

                  更新

                  另一个技巧是指定模型中的所有列(或使用额外的查询来使用 $this->getTableColumns() 从 这个答案,也可以在每次迁移后缓存以避免两次查询)然后添加一个局部作用域 函数

                  Another trick is to specify all columns in your model (or use an extra query to get all columns using $this->getTableColumns() from this answer, it can also be cached after each migration to avoid two queries) then add a local scope function

                  // The below code requires you to define all columns in $columns.
                  // A better approach is to query the schema of the table and cache it after each  
                  // migration, for more details: https://stackoverflow.com/a/56425794/3192276
                  
                  protected $columns = ['id','pseudo','email'];
                  
                  public function scopeExclude($query, $value = []) 
                  {
                      return $query->select(array_diff($this->columns, (array) $value));
                  }
                  

                  然后你可以这样做:

                  $users = User::where('gender', 'M')
                      ->where('is_active', 1)
                      ->exclude(['pseudo', 'email', 'age', 'created_at'])
                      ->toArray();
                  

                  这篇关于如何在使用 eloquent 时排除某些列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Laravel 合并关系 下一篇:如何使用 Laravel Eloquent 创建子查询?

                  相关文章

                  最新文章

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

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

                3. <small id='HcllA'></small><noframes id='HcllA'>

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