16

I want to get my table's column name. when i use model::all();

Users::all();

Illuminate\Database\Eloquent\Collection Object 
(
    [items:protected] => Array
        (
            [0] => Users Object
                (
                    [table:protected] => users
                    [hidden:protected] => Array
                        (
                            [0] => password
                            [1] => remember_token
                        )

                    [fillable] => Array
                        (
                        )

                    [connection:protected] => 
                    [primaryKey:protected] => id
                    [perPage:protected] => 15
                    [incrementing] => 1
                    [timestamps] => 1
                    [attributes:protected] => Array
                        (
                            [id] => 1
                            [first_name] => Mohammed Saqib
                            [last_name] => Rajput
                            [email] => rajput.saqib@hotmail.com
                            [dob] => 2015-06-18 00:00:00
                            [mobile] => 03006710419
                            [status] => inactive
                        )

                    [original:protected] => Array
                        (
                            [id] => 1
                            [first_name] => Mohammed Saqib
                            [last_name] => Rajput
                            [email] => rajput.saqib@hotmail.com
                            [dob] => 2015-06-18 00:00:00
                            [mobile] => 03006710419
                            [status] => inactive
                        )

                    [relations:protected] => Array
                        (
                        )

                    [visible:protected] => Array
                        (
                        )

                    [appends:protected] => Array
                        (
                        )

                    [guarded:protected] => Array
                        (
                            [0] => *
                        )

                    [dates:protected] => Array
                        (
                        )

                    [touches:protected] => Array
                        (
                        )

                    [observables:protected] => Array
                        (
                        )

                    [with:protected] => Array
                        (
                        )

                    [morphClass:protected] => 
                    [exists] => 1
                )

        )

)
Havelock
  • 6,913
  • 4
  • 34
  • 42
Mohammed Saqib Rajput
  • 1,331
  • 1
  • 14
  • 22

2 Answers2

31

You can make use of array_keys to get all attributes from a model.

$users = Users::all();
$user = $users->first();
$attributes = array_keys($user->toArray());

Alternatively you can use this approach to get the column names based from a certain table in your database.

Community
  • 1
  • 1
lukasgeiter
  • 147,337
  • 26
  • 332
  • 270
  • want to get key with where the collection is that possible??? $collection = Tracking::confirmedNotDel() ->groupBy('oppor_id') ->selectRaw('oppor_id, sum(logged_mins)/60 as logged_hours') ->get(); $collection = collect($collection); $data = $collection->where('oppor_id', $oppr->id); dd($data); //key of that object as i want to get row number – M Amir Shahzad Feb 22 '19 at 19:41
18

Now you can just use the ->keys() method from a collection.

$fruits = collect(['orange' => 15, 'lemon' => 75]);

$keys = $fruits->keys();

// ['orange', 'lemon']

See more at https://laravel.com/docs/master/collections#method-keys

Renan Coelho
  • 1,360
  • 2
  • 20
  • 30