Ich mache eine Funktion, die jedes Mal, wenn ich einen Kommentar zu einem Support-Ticket, sendet es eine E-Mail an den Benutzer, aber ich bekomme Trying to get property of non-object
, wenn ich die nur kommentiert dies geschieht, wenn ich aus der Benutzer-Konto angemeldet bin und nur in den Admin-Benutzer angemeldetLaravel Versuch, Eigenschaft von Nicht-Objekt beim Senden von E-Mails zu erhalten
AdminComment-Controller-Code
<?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');
}
}
hier wird der Mailer-Controller
<?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('[email protected]')
->to($this->to)->subject($this->subject);
});
}
}
Dank Mann änderte einige Sachen herum und es hat gut funktioniert :) – Yosef