Laravel Eloquent 使用查找获取追加属性

Laravel Eloquent Get Append Attributes With Find

我创建了一个具有 10 个附加属性的模型,但我无法使用 find() 方法获取它们。但是当我使用方法 toArray() 将返回的对象从 find() 转换为数组时,我可以看到它们,但我需要它们就像一个对象一样。 当我使用 find():

时打印了这个对象
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
)

当我使用 toArray 时:

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

)

这是我的例子 class:

<?php

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

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

这就是您通过属性访问器获得的自定义属性的本质。它们只会在使用时被评估(然后缓存以备后用)。当您将其转换为数组时,将加载所有 $appends 属性。

如果您有模型对象,您可以像访问任何其他属性一样访问它们:

$school->type;