[英]Laravel retrieve only the email address column in database for users table
嗨,我有说明的代码
$users = User::all();
但是我只想要集合中用户的电子邮件地址。 所以我尝试了:
$users = User::select('id', 'email')->first();
这不符合我的要求。 当我执行var_dump时,我只想查看用户的电子邮件地址。 有小费吗?
更新:我也插入了刀片服务器代码。 我使用具有下拉菜单的Vue组件,并在Vue方法中过滤数据并将过滤后的数据插入到userEmail下:
组件:
<commands-component
:entity-config="commandConfig(command.id, command.signature, command.hasUser, command.title, command.groupID, command.groupName)">
<input style="margin-right:10px;margin-bottom:10px;" type="button" class="btn btn-primary" v-bind:value="command.title" v-on:click='disableEmailText(command.hasUser)'>
</commands-component>
Vue:
var systemadmin = new Vue({
el: "#sysadmin",
data: function() {
return {
users: <?php echo $users ?>,
userData: [],
commands: <?php echo $commands ?>,
}
},
methods: {
commandConfig: function(ident, signature, hasUser, title, groupId, groupName){
var that = this;
var commandsGetUsers = [];
commandsGetUsers = _.map(that.users, function(value) {
var newValue = {};
newValue.value = value.id;
newValue.text = value.email;
return newValue;
});
return {
id: { text: 'commandModal' + ident, id: null },
modalTitle: title,
buttons: [
{
buttonTitle: 'Run Command',
buttonClass: 'btn btn-success pull-right',
submitUrl: {
url: '/admin/artisan/commands/run',
},
}
],
attributes: [
{name: "message", displayName: "Are you sure you want to proceed with the command?", type: "action-text", col: "12" },
{name: "signature", displayName: "Command Signature", type:'text', col:"6"},
{name: "userEmail", displayName: "Users", type: 'select', options: commandsGetUsers, col:'6'}
],
data: {
signature:signature,
hasUser:hasUser
}
};
},
$users = User::all()->pluck('email');
如果要所有用户:
$users = User::get(['id', 'email']);
单个用户(按ID):
$user = User::find(1, ['id', 'email']);
上面的将返回一个集合(第一种情况),或一个模型(第二种情况);
如果只想从结果中获取电子邮件:
$users->pluck('email');
$user->email; // obviously :)
当您将查询限制为仅返回所需的列时,可以得到更好的性能,这是通过将数组传递给get()
和find
方法来完成的。
如果您想要来自单个用户的电子邮件地址,请尝试以下操作:
$user = User::find(1)->email;
如果您想要所有用户/集合的电子邮件地址,请尝试以下操作:
$user = User::select('email')->get();
您可以使用only on集合。
$emails = User::all()->map(function($user) {
return $user->only(['email']);
});
您应该尝试这样:
$users = User::first(['id','email']);
要么
$users = User::get(['id','email']);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.