2

I want to get column names from a select query example:

$data = RoomModel::from('rooms as a')
    ->where('a.id', 1)
    ->where('a.isdelete', 0)
    ->join('roomclass as b', 'a.roomclass_id', 'b.id')
    ->join('nursestation as c', 'a.nursestation_id', 'c.id')
    ->select('a.id as ID', 'a.description as Description', 'b.customdesc as Room Class', 'c.customdesc as Nurse Station', 'a.isactive as Status');

Which will have a result of

array('ID','Description','Room Class', 'Nurse Station', 'Status')

This can be achieved using code igniter by using field_list:

$sql = "
    SELECT
    id as `ID`, 
    description as `Description`, 
    customdesc as `Nurse Station`,
    isactive as `Status`
    FROM mytable";

$query = $this->db->query($sql);
print_r($query->field_list());

which I can't find in Laravel.

Thanks in advance!!

3
  • Do you want alias name of column from your model? Commented Jun 30, 2021 at 5:13
  • If I get it right you want to retrieve filed names.then you can check this link stackoverflow.com/questions/37157270/… Commented Jun 30, 2021 at 5:30
  • @simpson yeah i'd like to get the field name but not from the whole table columns itself, i just want to get the field name of my selected columns in my query. Commented Jun 30, 2021 at 6:32

2 Answers 2

0
    $data = RoomModel::from('rooms as a')
                            ->where('a.id', 1)
                            ->where('a.isdelete', 0)
                            ->join('roomclass as b', 'a.roomclass_id', 'b.id')
                            ->join('nursestation as c', 'a.nursestation_id', 'c.id')
                            ->limit(1)->get('a.id as ID', 'a.description as Description', 'b.customdesc as Room Class', 'c.customdesc as Nurse Station', 'a.isactive as Status');

$attr = array_keys($data->getAttributes());
Sign up to request clarification or add additional context in comments.

4 Comments

the code seems to be also getting the result not the column names, what i want is the array of the columns I selected: array('ID','Description','Room Class', 'Nurse Station', 'Status')
thanks for the answer but what if the result is empty ? then this would'nt work ... unlike the field_list() in CI which still gives the column names even if the result is empty
@eman_invok05 Laravel has Schema::getColumnListing('table_name'); See this answer stackoverflow.com/questions/33512184/…
is it just returning my selected columns or the whole columns for the table ?
0

When using the model class to create a new query you will get an instance of Illuminate\Database\Eloquent\Builder (or a class derived from it).

To get the columns used in the select you need the query from within the builder:

$data = RoomModel::from('rooms as a')
    ...
    ->select('a.id as ID', 'a.description as Description', 'b.customdesc as Room Class', 'c.customdesc as Nurse Station', 'a.isactive as Status');

$columns = $data->getQuery()->columns;

If you are not using Eloquent, and have an instance of Illuminate\Database\Query\Builder you can get the columns property without the getQuery() call:

$data = DB::table('rooms as a')
    ...
$columns = $data->columns;

which will result in $columns being set to the array:

[
  "a.id as ID",
  "a.description as Description",
  "b.customdesc as Room Class",
  "c.customdesc as Nurse Station",
  "a.isactive as Status",
]

Unfortunately the alias are not resolved for you, even if you execute the query.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.