Laravel仅检索数据库中用户表的电子邮件地址列

时间:2017-11-16 06:26:42

标签: php laravel collections orm

您好我有代码

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



      },

5 个答案:

答案 0 :(得分:2)

$users = User::all()->pluck('email');

答案 1 :(得分:2)

如果您想要所有用户:

$users = User::get(['id', 'email']);

Id的单个用户:

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

以上将返回集合(第一个案例)或单个模型(第二个案例);

如果只想从结果中收到电子邮件:

$users->pluck('email');

$user->email; // obviously :)

当您将查询限制为仅返回所需的列时,通过将数组传递给get()find方法,可以获得更好的性能。

答案 2 :(得分:2)

如果您想要来自单个用户的电子邮件地址,请尝试以下操作:

$user = User::find(1)->email;

如果您想要所有用户/集合中的电子邮件地址,请尝试以下操作:

$user = User::select('email')->get();

答案 3 :(得分:0)

您可以仅使用集合。

    while(stateSearch.hasNextLine()){
    namearr.add(stateSearch.nextLine());
}
String[] arr = namearr.toArray(new String [0]);
System.out.println(arr);

答案 4 :(得分:0)

你应该试试这个:

$users = User::first(['id','email']);

$users = User::get(['id','email']);