Laravel: getting a a single value from a MySQL query

前端 未结 6 1784
[愿得一人]
[愿得一人] 2021-02-03 17:27

I\'m trying get a single value from MySQL database using laravel but the problem I\'m getting an array . this is my query result in MySQL command line:

select gr         


        
相关标签:
6条回答
  • 2021-02-03 17:40

    As of Laravel >= 5.3, best way is to use value:

    $groupName = \App\User::where('username',$username)->value('groupName');
    

    or

    use App\User;//at top of controller
    $groupName = User::where('username',$username)->value('groupName');//inside controller function
    

    Of course you have to create a model User for users table which is most efficient way to interact with database tables in Laravel.

    0 讨论(0)
  • 2021-02-03 17:42

    Using query builder, get the single column value such as groupName

      $groupName = DB::table('users')->where('username', $username)->pluck('groupName');
    

    For Laravel 5.1

     $groupName=DB::table('users')->where('username', $username)->value('groupName');
    

    Or, Using user model, get the single column value

     $groupName = User::where('username', $username)->pluck('groupName');
    

    Or, get the first row and then split for getting single column value

     $data = User::where('username', $username)->first();
     if($data)
       $groupName=$data->groupName;
    
    0 讨论(0)
  • 2021-02-03 17:56

    [EDIT] The expected output of the pluck function has changed from Laravel 5.1 to 5.2. Hence why it is marked as deprecated in 5.1

    In Laravel 5.1, pluck gets a single column's value from the first result of a query.

    In Laravel 5.2, pluck gets an array with the values of a given column. So it's no longer deprecated, but it no longer do what it used to.

    So short answer is use the value function if you want one column from the first row and you are using Laravel 5.1 or above.

    Thanks to Tomas Buteler for pointing this out in the comments.

    [ORIGINAL] For anyone coming across this question who is using Laravel 5.1, pluck() has been deprecated and will be removed completely in Laravel 5.2.

    Consider future proofing your code by using value() instead.

    return DB::table('users')->where('username', $username)->value('groupName');
    
    0 讨论(0)
  • 2021-02-03 17:59

    yet another edit: As of version 5.2 pluck is not deprecated anymore, it just got new behaviour (same as lists previously - see side-note below):

    edit: As of version 5.1 pluck is deprecated, so start using value instead:

    DB::table('users')->where('username', $username)->value('groupName');    
    

    // valid for L4 / L5.0 only
    DB::table('users')->where('username', $username)->pluck('groupName');
    

    this will return single value of groupName field of the first row found.


    SIDE NOTE reg. @TomasButeler comment: As Laravel doesn't follow sensible versioning, there are sometimes cases like this. At the time of writing this answer we had pluck method to get SINGLE value from the query (Laravel 4.* & 5.0).

    Then, with L5.1 pluck got deprecated and, instead, we got value method to replace it.

    But to make it funny, pluck in fact was never gone. Instead it just got completely new behaviour and... deprecated lists method.. (L5.2) - that was caused by the inconsistency between Query Builder and Collection methods (in 5.1 pluck worked differently on the collection and query, that's the reason).

    0 讨论(0)
  • 2021-02-03 18:02

    Edit:

    Sorry i forgot about pluck() as many have commented : Easiest way is :

    return DB::table('users')->where('username', $username)->pluck('groupName');
    

    Which will directly return the only the first result for the requested row as a string.

    Using the fluent query builder you will obtain an array anyway. I mean The Query Builder has no idea how many rows will come back from that query. Here is what you can do to do it a bit cleaner

    $result = DB::table('users')->select('groupName')->where('username', $username)->first();
    

    The first() tells the queryBuilder to return only one row so no array, so you can do :

    return $result->groupName;
    

    Hope it helps

    0 讨论(0)
  • 2021-02-03 18:05

    On laravel 5.6 it has a very simple solution:

    User::where('username', $username)->first()->groupName;
    

    It will return groupName as a string.

    0 讨论(0)
提交回复
热议问题