Laravel - 隐藏集合中的模型属性

时间:2018-02-19 18:07:21

标签: laravel eloquent

有没有办法在Laravel集合中隐藏模型的属性,比如table,connection,primaryKey等,只保留表的属性/列?

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

                        [withCount:protected] => Array
                            (
                            )

                        [perPage:protected] => 15
                        [exists] => 1
                        [wasRecentlyCreated] => 
                        [attributes:protected] => Array
                            (
                                ...
                            )

                        [original:protected] => Array
                            (
                                ...
                            )
                       ...
                    )

1 个答案:

答案 0 :(得分:2)

您不需要这样做,因为这些是Eloquent模型对象的属性,当您序列化对象或将其转换为JSON或数组时,它们将被忽略:

$model->toArray()