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

Bra*_*dge 37 php laravel eloquent

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

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

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

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

谢谢你的未来答案,祝你有愉快的一天.

Raz*_*zor 45

AFAIK SQL中没有构建选项来明确排除列,因此Laravel无法做到这一点.但你可以尝试这个技巧

更新

另一个技巧是指定模型中的所有列

protected $columns = array('id','pseudo','email'); // add all columns from you table

public function scopeExclude($query,$value = array()) 
{
    return $query->select( array_diff( $this->columns,(array) $value) );
}
Run Code Online (Sandbox Code Playgroud)

然后你可以这样做:

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

  • @Leon上面的模型函数`scopeExclude()`就是这样调用的.阅读https://laravel.com/docs/5.3/eloquent#local-scopes上的laravel范围 (5认同)
  • ->排除?这将导致方法不允许。 (2认同)

Chr*_*ius 36

我不知道以前的Laravel版本,但在5.4中你可以将这一行放在用户模型中

protected $hidden = ['pseudo', 'email', 'age', 'created_at'];
Run Code Online (Sandbox Code Playgroud)

然后User::find(1);将返回所有领域除外pseudo,email,age,和created_at.

但您仍然可以使用以下方法检索这些隐藏字段:

$user = User::find(1);
$email = $user['email']; // or $user->email;
Run Code Online (Sandbox Code Playgroud)

  • 也可以在Laravel 5.1中使用 (2认同)
  • 它从输出(toArray(),toJSON())中隐藏了它,但是仍然从数据库中加载它,因此当您不需要加载某些数据时,这种方法就没用了 (2认同)

saj*_*our 24

hidden在模型中使用数组是很好的方法,但是如果您不想一直隐藏列并用于makeVisible访问需要的列,则可以使用以下makeHidden函数在需要的地方从序列化中隐藏列:

$res = Model::where('your query')->get();
$res->makeHidden(['column_one','column_two');
return response()->json($res);
Run Code Online (Sandbox Code Playgroud)

  • 低估答案 (5认同)
  • 如果该列很大,这没有用,您仍然会查询它,这个想法不是查询它。我有一个表格,其中一列是形状几何图形,每个值大约为 500KB,我必须调用该模型的 100 个对象,我需要在查询中将它排除在外。 (5认同)

vuh*_*990 9

你可以hidden像这样使用数组:

class Promotion extends Model
{
    protected $table = 'promotion';
    protected $hidden = array('id');
}
Run Code Online (Sandbox Code Playgroud)


Man*_*n.A 8

我查看了@Razor 的答案

但是通过跳过 $columns 属性有一种非常方便的方法

/**
     * Scope a query to only exclude specific Columns
     *
     * @param  \Illuminate\Database\Eloquent\Builder $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeExclude($query, ...$columns) 
    {
        return $query->select( array_diff( $this->getTableColumns(),$columns) );
    }

    /**
     * Shows All the columns of the Corresponding Table of Model
     *
     * @author Manojkiran.A <manojkiran10031998@gmail.com>
     * If You need to get all the Columns of the Model Table.
     * Useful while including the columns in search
     * @return array
     **/
    public function getTableColumns()
    {
        return $this->getConnection()->getSchemaBuilder()->getColumnListing($this->getTable());
    }
Run Code Online (Sandbox Code Playgroud)

getTableColumns函数将获取表的所有列,因此您无需定义

$column 属性


Alm*_*itt 8

我有一个适合我的解决方案,它与已经提到的略有不同。

解决方案:

$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();
Run Code Online (Sandbox Code Playgroud)

推理:

为我:

  1. Razor 的答案不起作用,因为我收到以下错误:

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

  1. 然后,剩下的答案试图隐藏模型中的列。不幸的是,这会隐藏我的类中的每个方法,而这不是我想要的。

因此,最后,我修改了 Razor 的解决方案,使其无需隐藏每个方法的任何列即可工作。

我希望这可以帮助别人!


Raf*_*ier 5

我们从充满所有字段的模型中获得雄辩的对象,将其转换为数组,然后将其放入集合中。然后我们得到除数组$ fields中指定的所有字段以外的所有字段。

$fields = ['a', 'b', 'c', 'N'];
$object = Model::find($id);
return collect($object->toArray())->except($fields);
Run Code Online (Sandbox Code Playgroud)

更清楚地,让我们举一个例子:

// Array of fields you want to remove
$fields_to_remove = ['age', 'birthday', 'address'];

// Get the result of database
$user = User::find($id);

// Transform user object to array
$user = $user->toArray();

// Create a collection with the user inside
$collection = collect($user);

// Get all fields of our collection except these fields we don't want
$result = $collection->except($fields_to_remove);

// Return
return $result;
Run Code Online (Sandbox Code Playgroud)

上面的示例与第一个示例完全相同,但有更多解释。