Laravel 4.2模型突变器


laravel 4.2 model mutators

我现在正在做一个通知系统。我现在的问题是,所有的通知都是没有人情味的。

我想要的是获得保存到具有正确个性化语言的数据库的通知,因此,例如,我目前有一个通知,读起来像这样,

Joe Bloggs增加Billy Joel为项目经理。

我想让通知发送到每个不是Billy Joel的人,但是对于Billy Joel,我想要发送以下内容

Joe Bloggs加你为项目经理

目前我保存我的通知像这样的项目经理同步数据透视表,

$notification = new Notification;
    $notification->withURI($organisation->slug);
    $notification->withIsNotification(1);
    $notification->regarding($organisation);
    $notification->user_id = ResourceServer::getOwnerId();
    $notification->withType('updated');
    //die(print_r($changes));
    foreach($changes['attached'] as $k => $v) {
        //Loop through each new attachment to the pivot
        //look for who the attachement is from the user table and then
        //add the final notification details and fire the deliver method
        $addedUser = User::find($v);
        $addedUserName = $addedUser->first_name . " " . $addedUser->last_name;
        $notification->withBody($notifcationSenderName . " added " . $addedUserName . " to the organisation " . $organisation->name);
        $notification->deliver($notifyUsers);
    }

我想知道,当我通过它的模型从我的数据库查询用户时,我是否能够使用模型突变器来返回用户的第一和第二个名字,如果不需要个人通知,如果它返回"你"。这对突变体来说可能吗?如果可能,又是如何实现的?

虽然我不赞成这种方法,但您确实可以在访问器中这样做:

public function getNameAttribute()
{
    if (Auth::id() == $this->id) return 'you';
    return "{$this->first_name} {$this->last_name}";
}