繁体   English   中英

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}";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM