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

时间:2022-10-15 06:11:42

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:

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

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

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:

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

$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.

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

4 个解决方案

#1


29  

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

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

Update

更新

Another trick is to specify all columns in your model

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

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) );
}

Then you can do :

然后你可以这样做:

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

#2


21  

I don't know about previous Laravel version, but in 5.4 you can put this line in User model

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

protected $hidden = ['pseudo', 'email', 'age', 'created_at'];

and then

接着

User::find(1);

will return all fields except pseudo, email, age, and created_at.

将返回除pseudo,email,age和created_at之外的所有字段。

But you still can retrieve those hidden fields by using

但是你仍然可以通过使用来检索那些隐藏的字段

$user = User::find(1);
$email = $user['email'];

#3


4  

you can use hidden array like this:

你可以像这样使用隐藏数组:

class Promotion extends Model
{
    protected $table = 'promotion';
    protected $hidden = array('id');
}

#4


3  

We get the object eloquent from the model full with all fields, transform it to array and we put it inside of a collection. Than we get all fields except all fields specified in array $fields.

我们从模型中充分利用所有字段获得对象的雄辩,将其转换为数组,然后将其放入集合中。比我们获得除$ array中指定的所有字段之外的所有字段。

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

More clearly, let's give an example:

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

// 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;

This example above makes exactly the same thing of the first one, but it's more explained.

上面的这个例子与第一个例子完全相同,但它的解释更多。

#1


29  

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

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

Update

更新

Another trick is to specify all columns in your model

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

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) );
}

Then you can do :

然后你可以这样做:

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

#2


21  

I don't know about previous Laravel version, but in 5.4 you can put this line in User model

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

protected $hidden = ['pseudo', 'email', 'age', 'created_at'];

and then

接着

User::find(1);

will return all fields except pseudo, email, age, and created_at.

将返回除pseudo,email,age和created_at之外的所有字段。

But you still can retrieve those hidden fields by using

但是你仍然可以通过使用来检索那些隐藏的字段

$user = User::find(1);
$email = $user['email'];

#3


4  

you can use hidden array like this:

你可以像这样使用隐藏数组:

class Promotion extends Model
{
    protected $table = 'promotion';
    protected $hidden = array('id');
}

#4


3  

We get the object eloquent from the model full with all fields, transform it to array and we put it inside of a collection. Than we get all fields except all fields specified in array $fields.

我们从模型中充分利用所有字段获得对象的雄辩,将其转换为数组,然后将其放入集合中。比我们获得除$ array中指定的所有字段之外的所有字段。

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

More clearly, let's give an example:

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

// 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;

This example above makes exactly the same thing of the first one, but it's more explained.

上面的这个例子与第一个例子完全相同,但它的解释更多。