Laravel Getting attributes data

天涯浪子 提交于 2020-11-29 10:49:05

问题


I am new in laravel using Laravel 5.3. I am creating a check() function in laravel model for user login here i get all data form database useing default $this->all(); this return me a large multidymentional array .

Illuminate\Database\Eloquent\Collection Object
(
    [items:protected] => Array
        (
            [0] => App\wn_users Object
                (
                    [table:protected] => wn_users
                    [timestamps] => 
                    [fillable:protected] => Array
                        (
                            [0] => role_id
                            [1] => firstname
                            [2] => lastname
                            [3] => username
                            [4] => email
                            [5] => password
                            [6] => companyname
                            [7] => country_id
                            [8] => description
                            [9] => ip
                            [10] => update_date
                            [11] => status
                        )

                    [connection:protected] => 
                    [primaryKey:protected] => id
                    [keyType:protected] => int
                    [incrementing] => 1
                    [with:protected] => Array
                        (
                        )

                    [perPage:protected] => 15
                    [exists] => 1
                    [wasRecentlyCreated] => 
                    [attributes:protected] => Array
                        (
                            [user_id] => 1
                            [role_id] => 1
                            [firstname] => Aman kumar
                            [lastname] => --
                            [username] => Aman kumar
                            [email] => aman.imaxtechnologies@gmail.com
                            [password] => e10adc3949ba59abbe56e057f20f883e
                            [companyname] => Imax
                            [country_id] => 123
                            [description] => Testing
                            [ip] => 192.168.1.1
                            [update_date] => 2017-03-20
                            [status] => 0
                            [created_at] => 
                            [updated_at] => 
                        )

                    [original:protected] => Array
                        (
                            [user_id] => 1
                            [role_id] => 1
                            [firstname] => Aman kumar
                            [lastname] => --
                            [username] => Aman kumar
                            [email] => aman.imaxtechnologies@gmail.com
                            [password] => e10adc3949ba59abbe56e057f20f883e
                            [companyname] => Imax
                            [country_id] => 123
                            [description] => Testing
                            [ip] => 192.168.1.1
                            [update_date] => 2017-03-20
                            [status] => 0
                            [created_at] => 
                            [updated_at] => 
                        )

                    [casts:protected] => Array
                        (
                        )

                    [dates:protected] => Array
                        (
                        )

                    [dateFormat:protected] => 
                    [appends:protected] => Array
                        (
                        )

                    [events:protected] => Array
                        (
                        )

                    [observables:protected] => Array
                        (
                        )

                    [relations:protected] => Array
                        (
                        )

                    [touches:protected] => Array
                        (
                        )

                    [hidden:protected] => Array
                        (
                        )

                    [visible:protected] => Array
                        (
                        )

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

                )

        )

)

But i want to get only 'attributes:protected' Form whole array in laravel . I have already tried

echo $data = $this->getAttributes()['firstname']; but this returns error

Undefined index: firstname

Please help me to resolve my issue

Thanks in Advance for your help and time .


回答1:


A very simple way:

$arr = $this->all()->toArray();

var_dump($arr); // oh~ array data!



回答2:


So you have a collection with properties. And you can access them just like $collection->first()->firstname Or if you want to do some operation with all items use each method:

$collection = $collection->each(function ($item, $key) {
    $item->firstname .= ' Smith';
});


来源:https://stackoverflow.com/questions/42897045/laravel-getting-attributes-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!