Laravel 4.2在回复后发送电子邮件给用户

Laravel 4.2在回复后发送电子邮件给用户

问题描述:

so I have a ticket system, and what I want to do is when the user post a comment the other party should get an email, here is what I've accomplished so far:

In the CommentsController:

public function postComment($id) {
    $content = trim(Input::get('content'));
    if (empty($content)) {
        return Redirect::back();
    }
    if (Auth::user()->isAdmin() || $this->tickets->isTicketBelongsToUser(Auth::user()->id, $id)) {
        if (Input::hasFile('attachment')) {
            $attachmendId = Uploader::attach(Input::file('attachment'));
        }
        $comment = $this->comments->getNew(['content' => Input::get('content'), 'user_id' => Auth::user()->id, 'attachment_id' => isset($attachmendId) ? $attachmendId : null, 'ticket_id' => $id]);        

    //START geting the user id and send email//     

    $client = $this->users->$id;
    $this->userMailer->CommentRespond($id); 

    //END geting the user id and send email//       

        $this->comments->save($comment);
    }
    return Redirect::back()->withMessage('Your comment has been sent');
}

In the UserMailer.php:

 public function CommentRespond(User $user)
    {
        $view    = 'emails.new-comment';
        $subject = 'New Comment has been posted';
        $data    = [
        'name' => $user->name
        ];
        return $this->sendTo($user->email, $subject, $view, $data);
    }

Error:

 ErrorException (E_NOTICE)

Undefined property: Care\Repositories\Eloquent\UsersRepository::$93

I know it there is something wrong with the variable assigning but I couldn't find it out, so please if you can help that will be great.

thanks

所以我有一个票务系统,我想做的是当用户发表评论时另一方应该 收到一封电子邮件,这是我到目前为止所取得的成就: p>

在CommentsController中: p>

 公共函数postComment($ id)  {
 $ content = trim(Input :: get('content')); 
 if(empty($ content)){
 return Redirect :: back(); 
} 
 if(Auth ::  user() - > isAdmin()|| $ this-> tickets-> isTicketBelongsToUser(Auth :: user() - > id,$ id)){
 if(Input :: hasFile('attachment')  )){
 $ attachmendId = Uploader :: attach(输入::文件('附件')); 
} 
 $ comment = $ this-> comments-> getNew(['content'=> 输入:: get('content'),'user_id'=> Auth :: user() - > id,'attachment_id'=> isset($ attachmendId)?$ attachmendId:null,'ticket_id'=>  $ ID]);  
 
 //开始设置用户ID并发送电子邮件// 
 
 $ client = $ this-> users-> $ id; 
 $ this-> userMailer-> CommentRespond($ id  );  
 
 //结束用户ID并发送电子邮件// 
 
 $ this-> comments-> save($ comment); 
} 
返回Redirect :: back() - >  withMessage('你的评论已被发送'); 
} 
  code>  pre> 
 
 

在UserMailer.php中: p>

 公共功能CommentRespond(用户$用户)
 {
 $ view ='emails.new-comment'; 
 $ subject ='新评论已发布'; 
 $ data = [
'name'=  >  $ user-> name 
]; 
返回$ this-> sendTo($ user-> email,$ subject,$ view,$ data); 
} 
  code>  pre>  
 
 

错误: p>

  ErrorException(E_NOTICE)
 
未定义属性:Care \ Repositories \ Eloquent \ UsersRepository :: $ 93 
  code>  
 
 

我知道变量分配有问题但是我找不到它,所以如果你能提供帮助那就太好了。 p> 谢谢 p> div>

I think this is the culprit.

//START geting the user id and send email//     
//$client = $this->users->$id; <-- Wrong One
$client = $this->users->id;
$this->userMailer->CommentRespond($id); 

I suggest you convert in to this.

UPDATE: change the retrieval of user row to

$client = $this->users->getById($id);
$this->userMailer->CommentRespond($client); 

then update CommentRespond

public function CommentRespond($user)
{
    $view    = 'emails.new-comment';
    $subject = 'New Comment has been posted';
    $data    = [
    'name' => $user['name']
    ];
    return $this->sendTo($user['email'], $subject, $view, $data);
}