2015-02-09 105 views
0

我創建了一個包含10個附加屬性的模型,我無法使用find()方法獲取它們。但是當我使用方法toArray()將返回的對象從find()轉換爲數組時,我可以看到它們,但我需要它們像一個對象。當我使用find()方法 這個對象是印:Laravel Eloquent通過查找附加屬性

School Object 
(
    [table:protected] => schools 
    [fillable:protected] => Array 
     (
      [0] => name 
      [1] => type_id 
      [2] => description 
      [3] => info_specialties 
      [4] => contacts 
      [5] => cover_name 
      [6] => financing_id 
      [7] => city 
     ) 

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

    [appends:protected] => Array 
     (
      [0] => type 
      [1] => short_type 
      [2] => school_url 
      [3] => cover_photo_url 
      [4] => cover_photo_thumbnail_url 
      [5] => city 
      [6] => municipality 
      [7] => appended_district_id 
      [8] => district 
      [9] => description_without_tags 
     ) 

    [district_id] => 
    [cover_photo] => 
    [connection:protected] => 
    [primaryKey:protected] => id 
    [perPage:protected] => 15 
    [incrementing] => 1 
    [timestamps] => 1 
    [attributes:protected] => Array 
     (
      [id] => 24 
      [type_id] => 3 
      [name] => adasdasd 
      [description] => 
asdadasdasdasdqwdqd\qw\[dqw\d 



      [info_specialties] => 
qwqwdqwdqwdqwdqwd 



      [contacts] => 
qwdqwdqwdqwd 



      [cover_name] => SAbjfpe4m7.jpg 
      [financing_id] => 1 
      [city_id] => 18 
      [created_at] => 2015-01-31 20:56:06 
      [updated_at] => 2015-02-04 18:50:13 
     ) 

    [original:protected] => Array 
     (
      [id] => 24 
      [type_id] => 3 
      [name] => adasdasd 
      [description] => 
asdadasdasdasdqwdqd\qw\[dqw\d 



      [info_specialties] => 
qwqwdqwdqwdqwdqwd 



      [contacts] => 
qwdqwdqwdqwd 



      [cover_name] => SAbjfpe4m7.jpg 
      [financing_id] => 1 
      [city_id] => 18 
      [created_at] => 2015-01-31 20:56:06 
      [updated_at] => 2015-02-04 18:50:13 
     ) 

    [relations:protected] => Array 
     (
     ) 

    [hidden:protected] => Array 
     (
     ) 

    [visible:protected] => Array 
     (
     ) 

    [dates:protected] => Array 
     (
     ) 

    [touches:protected] => Array 
     (
     ) 

    [observables:protected] => Array 
     (
     ) 

    [with:protected] => Array 
     (
     ) 

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

當我使用指定者:

Array 
(
    [id] => 24 
    [type_id] => 3 
    [name] => adasdasd 
    [description] => 
asdadasdasdasdqwdqd\qw\[dqw\d 



    [info_specialties] => 
qwqwdqwdqwdqwdqwd 



    [contacts] => 
qwdqwdqwdqwd 



    [cover_name] => SAbjfpe4m7.jpg 
    [financing_id] => 1 
    [city_id] => 18 
    [created_at] => 2015-01-31 20:56:06 
    [updated_at] => 2015-02-04 18:50:13 
    [type] => qdasdasd 
    [short_type] => asdasdasd 
    [school_url] => http://localhost:8000/school/24 
    [cover_photo_url] => http://localhost:8000/storage/cover_photos/SAbjfpe4m7.jpg 
    [cover_photo_thumbnail_url] => http://localhost:8000/storage/cover_photos/thumbnails/SAbjfpe4m7.jpg 
    [city] => sdasdasd 
    [municipality] => ÐÑкаква община2 от нÑкакъв облаÑÑ‚2 
    [appended_district_id] => 6 
    [district] => ÐÑкакъв облаÑÑ‚2 
    [description_without_tags] => asdadasdasdasdqwdqd\qw\[dqw\d 

) 

這裏是我的類的例子:

<?php 

class School extends Eloquent { 
    protected $appends = array('type'); 

    public function getTypeAttribute() 
    { 
     return Type::find($this->type_id)->name; 
    } 
} 

回答

1

這是本質您使用屬性訪問器獲得的自定義屬性。它們只在使用時才被評估(然後被緩存以供以後使用)。將其轉換爲數組時,將加載所有$appends屬性。

如果你有你的模型對象你可以訪問他們像任何其他屬性:

$school->type;