Home > Software engineering >  get Field name from the query builder result
get Field name from the query builder result

Time:06-21

I need to get field names from the query builder result. For a single table I could use

DB::getSchemaBuilder()->getColumnListing('table_name');

but what i need is from the query builder result.

   DB::table('users as a')
->leftJoin('userwork AS uk','uk.WORK_ID', '=','a.WORK_ID')
->selectRaw("a. name ,uk.work_name as work ,concat(' ',uk.work_phone) as phone")
->get();

I want to extract the attribute name to get the result like below

['name','work','phone'];

CodePudding user response:

I end up using array_keys method after converting the first result of the query builder to array.

$db_fields=array_keys((array)$items->first());

CodePudding user response:

Do you need like this?

DB::table('users as a')
->leftJoin('userwork AS uk','uk.WORK_ID', '=','a.WORK_ID')
->select("a. name ,uk.work_name as work ,concat(' ',uk.work_phone) as phone")
->get();

just using select. You could write on your controller.

  • Related