php 如何从laravel中的数组中获取键名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27854509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:33:54  来源:igfitidea点击:

How to get keys name from array in laravel

phparrayslaravellaravel-4

提问by Mohammed Saqib Rajput

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

我想获取我的表的列名。当我使用 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] => [email protected]
                            [dob] => 2015-06-18 00:00:00
                            [mobile] => 03006710419
                            [status] => inactive
                        )

                    [original:protected] => Array
                        (
                            [id] => 1
                            [first_name] => Mohammed Saqib
                            [last_name] => Rajput
                            [email] => [email protected]
                            [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
                )

        )

)

回答by lukasgeiter

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

您可以使用array_keys来从模型中获取所有属性。

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

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

或者,您可以使用此方法从数据库中的某个表中获取列名。

回答by Renan Coelho

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

现在您可以只使用->keys()集合中的方法。

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

$keys = $fruits->keys();

// ['orange', 'lemon']

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

https://laravel.com/docs/master/collections#method-keys查看更多