Laravel: getting a a single value from a MySQL query

前端 未结 6 1783
[愿得一人]
[愿得一人] 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.

提交回复
热议问题