2015-02-09 65 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;