How to exclude certains columns while using eloquent

前端 未结 8 1267
梦谈多话
梦谈多话 2020-12-02 21:59

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:

$us         


        
相关标签:
8条回答
  • 2020-12-02 22:48

    I have a solution that worked for me, which is slightly different than those already stated.

    Solution:

    $all_columns = Schema::getColumnListing('TABLE_NAME');
    $exclude_columns = ['COLUMN_TO_EXCLUDE_1', 'COLUMN_TO_EXCLUDE_2'];
    $get_columns = array_diff($all_columns, $exclude_columns);
    
    return User::select($get_columns)->get();
    

    Reasoning:

    For me:

    1. Razor's answer didn't work as I got the following error:

    BadMethodCallException with message 'Call to undefined method App/CaseStudy::exclude()'

    1. Then, the remaining answers were attemping to hide the columns within the model. Unfortunately, that would hide them for each method in my class and this isn't something that I wanted.

    So, in the end, I modified Razor's solution so that it would work without having to hide any of the columns for each method.

    I hope this helps someone!

    0 讨论(0)
  • 2020-12-02 22:55

    you can use hidden array like this:

    class Promotion extends Model
    {
        protected $table = 'promotion';
        protected $hidden = array('id');
    }
    
    0 讨论(0)
提交回复
热议问题