【问题标题】:Laravel how to return single column value using Query BuilderLaravel如何使用查询生成器返回单列值
【发布时间】:2020-10-21 21:44:18
【问题描述】:

我想使用 SQL Query 中的数据,在这里进一步解释是我的代码:

    $myquery = DB::table('attendances')->select('user_id')->where('date_only', '=', $newdate)->orderBy('logon','asc')->get();

    $myquery->user_id;

现在我希望我的 $myquery 的值是 USER_ID 的值,如果我使用上面的代码 -$myquery->user_id 会出错;

【问题讨论】:

  • 我认为这是因为您没有检索到单个结果。所以,如果你想要user_id,你可以遍历$myquery,或者选择第一个结果)->first()
  • 正如@GiuServ 所说:get() 将返回一个出勤对象数组,即使只有一个对象与 where 条件匹配。其中 first() 将从结果中返回第一个匹配的对象。甚至不止一个对象匹配 where 条件。

标签: php laravel


【解决方案1】:

编辑:从版本 5.1 开始,pluck 已被弃用,因为 5.2 它的含义完全不同(就像以前的 lists 一样),所以使用 @987654325 @ 方法。


您正在尝试获取数组的属性,因此显然您遇到了错误。

如果您只需要单个字段,则使用pluck(默认返回单个值 - 字符串):

// let's assume user_id is 5

DB::table('attendances')
  ->where('date_only', '=', $newdate)
  ->orderBy('logon','asc')
  ->pluck('user_id');  // "5"

当你需要整行时,然后first(返回stdObject):

$att = DB::table('attendances')
  ->where('date_only', '=', $newdate)
  ->orderBy('logon','asc')
  ->first();

$att->user_id; // "5"
$att->any_other_column;

get 是您想要多个结果(返回简单的 stdObjects 数组)时:

$result = DB::table('attendances')
  ->where('date_only', '=', $newdate)
  ->orderBy('logon','asc')
  ->get();

$result; // array(0 => stdObject {..}, 1 => stdObject {..}, ...)
$result[0]->user_id; // "5"

【讨论】:

  • 您从哪里读到 pluck 在 5.1 中已弃用? value() 是替代品吗?
  • @GiuServ 使用源,Luke! github.com/laravel/framework/blob/5.1/src/Illuminate/Database/… 是的,value 是新的pluck
  • 没关系,我只是明白 pluck 功能从 5.1 开始发生了变化。我直接从 5.3 开始,之前没有得到它没有相同的行为!
  • laravel.com/docs/5.6/queries 从表中检索单行/单列 $email = DB::table('users')->where('name', 'John')->value('email'); 检索列值列表 $titles = DB::table('roles')->pluck('title');
【解决方案2】:

这适用于 Laravel 5.3:

$user_id = DB::table('attendances')->select('user_id')->where('date_only', '=', $newdate)->orderBy('logon','asc')->value('user_id');

【讨论】:

  • select('user_id') 不需要
  • 它可以工作,但如果没有找到行,你会得到一个异常“Call value() on null”
【解决方案3】:

要仅获取单个列的值列表,您可以这样做。

$result = CustomerInfo::select('eventDate')->pluck('eventDate');

这里 $result 会给你这样的结果:

["2019-03-25","2020-03-31","2019-03-31","2019-04-24","2019-11-26"]

希望对某人有所帮助

【讨论】:

  • 谢谢,这正是我想要的
【解决方案4】:

这将只返回 user_id

$myquery = DB::table('attendances')->where('date_only', $newdate)->select('user_id')->pluck('user_id')->first();

【讨论】:

    【解决方案5】:

    根据latest documentation: 5.6

    DB::table('users')->select('name');
    

    会成功的。

    【讨论】:

      【解决方案6】:

      我认为有些混乱;但我使用的是 Laravel 5.3,并且 pluck 和 value 函数没有被弃用,如上所示。

      更多说明: 当您只需要所有值时,将使用 pluck() 而不是 get() 当您只需要一个值时,将使用 value() 而不是 first()

      使用方法 all() 来获取 itmes 的数组。 用法:

      \App\User::whereIn('mobile',['971700000', '965000000'])->select('id')->pluck('id')->all();
      

      【讨论】:

        【解决方案7】:

        如果您已经在数组中有数据并且您只想过滤此类数据,您也可以这样做

          0 => "first_name"
          1 => "middle_name"
        ]
        
        
        $users_data = User::where('id', '1')->get($filter_data );
        return $users_data; ```
        
        <pre> In laravel 5.4 works perfect for me </pre>
        

        【讨论】:

          【解决方案8】:

          DB::table('table_name')-&gt;select('column_name');

          【讨论】:

          • 应该解释你的答案
          【解决方案9】:
           $myquery = DB::table('attendances')->select('user_id')
                      ->where('date_only', '=', $newdate)->orderBy('logon','asc')->get();
          

          默认情况下,返回的数据将是对象数组。你需要遍历它。

          例如

          foreach($myquery as $i)
          {
              echo $i->user_id;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2021-06-04
            • 2021-06-23
            • 2017-03-29
            • 2020-12-25
            • 2015-03-26
            • 2016-12-17
            相关资源
            最近更新 更多