在laravel中查看对象内的对象

时间:2014-03-05 23:23:52

标签: php date laravel laravel-4 blade

我的laravel视图中有一个对象应该在对象中有一个对象,但是我无法使用对象表示法访问子对象。我可以用数组表示法访问子属性。

在我看来,我可以通过以下方式访问父属性:

$model->Model

但这不能用于访问[specs]中的子属性:

$model->specs->updated_at

这有效

$model['specs']['updated_at']

我正在尝试获取specs子级中的updated_at值,但是$model['specs']['updated_at']返回的值不正确。对象中的值为“0000-00-00 00:00:00”,但$model['specs']['updated_at']返回的值为“-0001-11-30 00:00:00”。这就是为什么我试图使用对象表示法来查看是否返回正确的值。

这是对象上的print_r:

Pricing Object
(
    [primaryKey:protected] => ModelID
    [table:protected] => Pricing
    [connection:protected] => design
    [perPage:protected] => 15
    [incrementing] => 1
    [timestamps] => 1
    [attributes:protected] => Array
        (
            [ModelID] => 2762
            [MakeID] => 151
            [Model] => 240MK
            [Cost] => 17076
            [MarkUp] => 0.134690
            [OptionID] => A
            [Surcharge] => 0.00
            [MAP] => 21999
            [galleryId] => 1158
            [Base] => 18300
            [Discount] => 3000
            [Mandatory_Options] => 3500, 150, 30, 96
            [Option_Discount] => 2000
            [created_at] => 0000-00-00 00:00:00
            [updated_at] => 2014-02-17 14:22:18
        )

    [original:protected] => Array
        (
            [ModelID] => 2762
            [MakeID] => 151
            [Model] => 240MK
            [Cost] => 17076
            [MarkUp] => 0.134690
            [OptionID] => A
            [Surcharge] => 0.00
            [MAP] => 21999
            [galleryId] => 1158
            [Base] => 18300
            [Discount] => 3000
            [Mandatory_Options] => 3500, 150, 30, 96
            [Option_Discount] => 2000
            [created_at] => 0000-00-00 00:00:00
            [updated_at] => 2014-02-17 14:22:18
        )

    [relations:protected] => Array
        (
            [specs] => Spec Object
                (
                    [primaryKey:protected] => ModelID
                    [table:protected] => Specs
                    [connection:protected] => design
                    [perPage:protected] => 15
                    [incrementing] => 1
                    [timestamps] => 1
                    [attributes:protected] => Array
                        (
                            [ModelID] => 2762
                            [Dry] => 5292
                            [GVWR] => 0
                            [Cargo] => 1908
                            [Tongue] => 0
                            [Axle] => 0
                            [Height] => 131
                            [Length] => 331
                            [Width] => 0
                            [Interior] => 0
                            [Fresh] => 43
                            [Gray] => 60
                            [Black] => 30
                            [Awning] => 0
                            [Type] => A
                            [Bunks] => H
                            [Fiberglass] => I
                            [Slideout] => 1
                            [Updatestamp] => 012513
                            [Sleeps] => 3
                            [Vents] => 0
                            [Exterior_Kitchen] => L
                            [Exterior_Doors] => 1
                            [Awning_Size1] => 
                            [Awning_Size2] => 
                            [Awning_Size3] => 
                            [Awning_Size4] => 
                            [Awning_Size5] => 
                            [created_at] => 0000-00-00 00:00:00
                            [updated_at] => 0000-00-00 00:00:00
                        )

                    [original:protected] => Array
                        (
                            [ModelID] => 2762
                            [Dry] => 5292
                            [GVWR] => 0
                            [Cargo] => 1908
                            [Tongue] => 0
                            [Axle] => 0
                            [Height] => 131
                            [Length] => 331
                            [Width] => 0
                            [Interior] => 0
                            [Fresh] => 43
                            [Gray] => 60
                            [Black] => 30
                            [Awning] => 0
                            [Type] => A
                            [Bunks] => H
                            [Fiberglass] => I
                            [Slideout] => 1
                            [Updatestamp] => 012513
                            [Sleeps] => 3
                            [Vents] => 0
                            [Exterior_Kitchen] => L
                            [Exterior_Doors] => 1
                            [Awning_Size1] => 
                            [Awning_Size2] => 
                            [Awning_Size3] => 
                            [Awning_Size4] => 
                            [Awning_Size5] => 
                            [created_at] => 0000-00-00 00:00:00
                            [updated_at] => 0000-00-00 00:00:00
                        )

                    [relations:protected] => Array
                        (
                        )

                    [hidden:protected] => Array
                        (
                        )

                    [visible:protected] => Array
                        (
                        )

                    [appends:protected] => Array
                        (
                        )

                    [fillable:protected] => Array
                        (
                        )

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

                    [dates:protected] => Array
                        (
                        )

                    [touches:protected] => Array
                        (
                        )

                    [with:protected] => Array
                        (
                        )

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

        )

    [hidden:protected] => Array
        (
        )

    [visible:protected] => Array
        (
        )

    [appends:protected] => Array
        (
        )

    [fillable:protected] => Array
        (
        )

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

    [dates:protected] => Array
        (
        )

    [touches:protected] => Array
        (
        )

    [with:protected] => Array
        (
        )

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

1 个答案:

答案 0 :(得分:0)

<?php
class one
{

    public $hello="hello";
}
class two
{

    public $welcome;

    public function __construct()
    {
        $this->welcome  = new one();
    }
}
$two=new two();
$two->welcome->hello;
?>