如何在使用 eloquent 时排除某些列

How to exclude certains columns while using eloquent(如何在使用 eloquent 时排除某些列)
本文介绍了如何在使用 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'];
}

  • 使用makeHidden功能

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

  • 有关更多详细信息,请参阅其他答案... 但是 有时您不想将大量数据(地理空间、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 时排除某些列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

    本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

    相关文档推荐

    DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
    PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
    mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
    Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
    Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
    Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)