【问题标题】:Laravel retrieve only the email address column in database for users tableLaravel 仅检索数据库中用户表的电子邮件地址列
【发布时间】:2017-11-16 06:26:41
【问题描述】:

嗨,我有代码说明

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



      },

【问题讨论】:

  • 所有用户或单个用户的电子邮件?你能详细说明一下吗?
  • 所有用户的电子邮件地址:)
  • 使用 pluck $users = User::pluck('email')-&gt;toArray(); 将所有用户的电子邮件作为数组检索。
  • @Rits 然后它在我的刀片模板上给了我错误 - 数组到字符串的转换。我正在使用用户电子邮件来填充下拉列表

标签: php laravel collections orm


【解决方案1】:

如果你想要单个用户的电子邮件地址,试试这个:

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

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • pluck 正在收集中,而不是收集中。您应该在 map 函数中使用 pluck。
    • @KrisRoofe 他没有处理收藏集,因为我知道他只想返回所有用户的电子邮件地址。 User::all() 返回单个集合。
    • all() 方法将返回一个集合的集合。
    • 对不起,这里我不是很清楚。也许你是对的。我需要测试一下。
    • @KrisRoofe 如果您查看all 的函数签名,它会返回Illuminate\Database\Eloquent\Collection|static
    【解决方案3】:

    如果你想要所有用户:

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

    按 ID 的单个用户:

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

    上面将返回一个集合(第一种情况),或单个模型(第二种情况);

    如果只想从结果中获取邮件:

    $users->pluck('email');
    
    $user->email; // obviously :)
    

    当您将查询限制为仅返回您需要的列时,您会获得更好的性能,这是通过将数组传递给 get()find 方法来完成的。

    【讨论】:

      【解决方案4】:

      您可以使用 only on 集合。

      $emails = User::all()->map(function($user) {
          return $user->only(['email']);
      });
      

      【讨论】:

      • 这个在我做 var_dump 时效果最好。我只得到电子邮件地址,但这会导致刀片模板中的下拉列表出现问题 - 下拉列表值(用户电子邮件)消失
      • @你能显示你的刀片代码吗?我不知道你想在你的刀片 php 中呈现什么。上面的代码只是返回一个电子邮件数组。
      • @抱歉,我之前没用过 vue。我无法弄清楚 js 代码在做什么。
      【解决方案5】:

      你应该试试这个:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-06-26
        • 1970-01-01
        • 1970-01-01
        • 2012-10-03
        • 2017-03-19
        • 1970-01-01
        • 1970-01-01
        • 2022-11-17
        相关资源
        最近更新 更多