Zain
Zain

Reputation: 33

how to get all column values in string in Laravel

how to get all column values in string in laravel query, my query is,

$profile = DB::table('app_user')->where([
      'email' => $request->email,
      'password' => md5($request->password),
      'app_id' => $request->app_id
  ])->select('id as app_user_id','id','name','email','user_image','activate')
    ->first();

this return in object

 "app_user_id": 2857687,
 "id": 2857687,
 "name": "zahid"

I want to output like this

"app_user_id": "2857687",
"id": "2857687",
"name": "zahid"

I am using laravel 5.4

please help thanks in advance

Upvotes: 1

Views: 1108

Answers (3)

Jignesh Joisar
Jignesh Joisar

Reputation: 15085

convert into unsigned integer using mysql CAST function

$profile = DB::table('app_user')->where([
      'email' => $request->email,
      'password' => md5($request->password),
      'app_id' => $request->app_id
  ])->select(\DB::raw('CAST(id AS UNSIGNED) as app_user_id'),\DB::raw('CAST(id AS UNSIGNED) as id'),'name','email','user_image','activate')
    ->first();

Upvotes: 1

Janiis
Janiis

Reputation: 1576

You can convert integer to string in mysql query like this:

.. ->selectRaw('CAST(id as CHAR(50)) as app_user_id', 'name', ...) ->first();

Upvotes: 0

Shahadat Hossain
Shahadat Hossain

Reputation: 979

you can cast MySQL data like this.

$profile = DB::table('app_user')->where([
      'email' => $request->email,
      'password' => md5($request->password),
      'app_id' => $request->app_id
  ])->select(DB::raw('CAST(id AS varchar) as app_user_id, CAST(id as varchar) as id, name, email, user_image, activate'))
    ->first()

Upvotes: 1

Related Questions