简体   繁体   中英

laravel 4.2 model mutators

I am working on a notification system at the moment. The problem I am having at the moment, is that all notifications are impersonal.

What I would like is to get notifications saving to the database that have the correct personalisation language, so for example, I currently have a notification that reads like this,

Joe Bloggs added Billy Joel as a project manager.

What I would like is for the notification to go to every that is not Billy Joel, but for Billy Joel I want the following, to be sent

Joe Bloggs added you as a project manager

Currently I save my notifications like this for the projects managers on the sync of the pivot table,

$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);
    }

I was wondering that as I am querying the User from my database through it's model, would I be able to use model mutators to either return the users first and second name, if there does not need to be a personal notification and if it does then return "You". Is this possible with mutators and if so how?

While I'm not endorsing this approach, you sure can do it in an accessor:

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

    return "{$this->first_name} {$this->last_name}";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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