我正在制作一项功能,每当我向支持服务单添加评论时,它都会向用户发送一封电子邮件,但是当我提交评论时我才会收到Trying to get property of non-object
这只会发生如果我已注销用户帐户并仅登录管理员用户
AdminComment控制器代码
<?php
namespace App\Http\Controllers;
use App\Comment;
use App\Mailers\AppMailer;
use App\Ticket;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class AdminCommentController extends Controller
{
public function postComment(Request $request, AppMailer $mailer)
{
$this->validate($request, [
'comment' => 'required',
]);
$comment = Comment::create([
'ticket_id' => $request->input('ticket_id'),
'user_id' => Auth::user()->id,
'comment' => $request->input('comment'),
]);
$mailer->sendTicketComments($comment->ticket->user, Auth::user(), $comment->ticket, $comment);
return redirect()->back()->with('warning', 'There was a problem sending your comment to the customer via email');
}
}
这是邮件控制器
<?php
namespace App\Mailers;
use App\Status;
use App\Ticket;
use Illuminate\Contracts\Mail\Mailer;
class AppMailer
{
protected $mailer;
/**
* email to send to.
*
* @var [type]
*/
protected $to;
/**
* Subject of the email.
*
* @var [type]
*/
protected $subject;
/**
* view template for email.
*
* @var [type]
*/
protected $view;
/**
* data to be sent along with the email.
*
* @var array
*/
protected $data = [];
public function __construct(Mailer $mailer)
{
$this->mailer = $mailer;
}
/**
* Send Ticket information to the user.
*
* @param User $user
* @param Ticket $ticket
*
* @return method deliver()
*/
public function sendTicketInformation($user, Ticket $ticket)
{
$statuses = Status::all();
$this->to = $user->email;
$this->subject = "TechWiseDirect Support Ticket - [Reference #: $ticket->ticket_id]";
$this->view = 'users.emails.ticket_info';
$this->data = compact('user', 'ticket', 'statuses');
return $this->deliver();
}
/**
* Send Ticket Comments/Replies to Ticket Owner.
*
* @param User $ticketOwner
* @param User $user
* @param Ticket $ticket
* @param Comment $comment
*
* @return method deliver()
*/
public function sendTicketComments($ticketOwner, $user, Ticket $ticket, $comment)
{
$this->to = $ticketOwner->email;
$this->subject = "RE:[Ticket ID: $ticket->ticket_id]";
$this->view = 'users.emails.ticket_comments';
$this->data = compact('ticketOwner', 'user', 'ticket', 'comment');
return $this->deliver();
}
/**
* Do the actual sending of the mail.
*/
public function deliver()
{
$this->mailer->send($this->view, $this->data, function ($message) {
$message->from('test@test')
->to($this->to)->subject($this->subject);
});
}
}
答案 0 :(得分:0)
我认为错误发生在forEach
。这是因为您已退出帐户。我的猜测是你没有使用过防护,只有一个主要的用户模型被定义为laravel。