【问题标题】:laravel 4.2 model mutatorslaravel 4.2 模型修改器
【发布时间】:2015-11-05 08:21:39
【问题描述】:

我目前正在开发一个通知系统。我目前遇到的问题是所有通知都是非个人的。

我想要的是将通知保存到具有正确个性化语言的数据库中,例如,我目前有一个这样的通知,

Joe Bloggs 将 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);
    }

我想知道,当我通过它的模型从我的数据库中查询用户时,如果不需要个人通知并且如果然后它会返回“你”。突变器是否可以实现这一点?如果可以,如何实现?

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    虽然我不支持这种方法,但您肯定可以在访问器中做到这一点:

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

    【讨论】:

    • @Zuhran - 而auth() 助手在 4.2 中不可用,the id method is。如果没有登录用户,它会更安全。
    猜你喜欢
    • 2016-01-07
    • 2015-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-29
    相关资源
    最近更新 更多