2014-03-05 96 views
0

我在我的laravel視圖中有一個對象應該在對象中有一個對象,但我無法用對象表示法訪問子對象。我可以使用數組表示法訪問子屬性。在laravel中查看對象的視圖

在我看來,我可以訪問父與屬性:

$model->Model

,但是這不會工作訪問[規格]子屬性:

$model->specs->updated_at

這作品

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

我正在嘗試獲取規格子中的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] => 
) 
+0

嘗試使用'DD($模型 - >規範)'檢查對象,你可以看到所有的屬性。 –

+0

我可以通過'dd($ model-> specs)看到規格的屬性',但我仍然無法通過'$ model-> specs-> ModelID'訪問它的屬性。 – Chris

+0

發佈你的關係方法併發布你在做什麼時得到的東西'DD($模型 - >規格)'。 –

回答

0
<?php 
class one 
{ 

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

    public $welcome; 

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