【问题标题】:How to get keys name from array in laravel如何从laravel中的数组中获取键名
【发布时间】:2015-03-07 10:00:04
【问题描述】:

我想获取我的表的列名。当我使用模型::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
                )

        )

)

【问题讨论】:

    标签: php arrays laravel laravel-4


    【解决方案1】:

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

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

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

    【讨论】:

    • 想要获取可能的集合的关键? $collection = Tracking::confirmedNotDel() ->groupBy('oppor_id') ->selectRaw('oppor_id, sum(logged_mins)/60 as logged_hours') ->get(); $collection = 收集($collection); $data = $collection->where('oppor_id', $oppr->id); dd($数据); //该对象的键,因为我想获取行号
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2020-12-12
      • 1970-01-01
      • 2017-04-15
      • 1970-01-01
      • 1970-01-01
      • 2019-06-22
      • 2020-12-12
      • 2018-08-09
      • 1970-01-01
      相关资源
      最近更新 更多