Laravel: getting a a single value from a MySQL query

前端 未结 6 1794
[愿得一人]
[愿得一人] 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: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;
    

提交回复
热议问题