我正在制作一项功能,每当我向RMA票证添加评论时,它都会向用户发送电子邮件,但我在提交评论时收到Trying to get property of non-object
下面我粘贴了CommentController和Email函数中的代码 如果你想了解更多信息,我会发布我可以发布的信息
我收到此代码的错误 - $mailer->sendRmaTicketComments($comment->ticket->user, Auth::user(), $comment->ticket, $comment);
CommentController代码
<?php
namespace App\Http\Controllers\Rma;
use Auth;
use App\User;
use App\Rma\RmaTicket;
use App\Rma\RmaComments;
use App\Mailers\AppMailer;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class RmaCommentController extends Controller
{
public function postComment(Request $request, AppMailer $mailer)
{
$this->validate($request, [
'comment' => 'required',
'user_id' => 'required',
]);
$comment = RmaComments::create([
'rma_ticket_id' => $request->input('ticket_id'),
'user_id' => $request->input('user_id'),
'comment' => $request->input('comment'),
]);
$mailer->sendRmaTicketComments($comment->ticket->user, Auth::user(), $comment->ticket, $comment);
return redirect()->back()->with('successPanel', 'Thank you for your message please allow up to 48 hours for a response.');
}
}
邮件功能代码
<?php
namespace App\Mailers;
use App\Rma\RmaTicket;
use App\Rma\RmaStatus;
use App\SupportTickets\SupportTicket;
use App\SupportTickets\SupportTicketStatus;
use App\User;
use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Support\Facades\Mail;
class AppMailer
{
protected $mailer;
/**
* Get the email address
*
* @var [type]
*/
protected $to;
/**
* Set the subject for the email
*
* @var [type]
*/
protected $subject;
/**
* Set the template for the email
*
* @var [type]
*/
protected $view;
/**
* Set the data for the email
*
* @var array
*/
protected $data = [];
public function __construct(Mailer $mailer)
{
$this->mailer = $mailer;
}
/**
* Send RMA Comments/Replies to RMA Owner
*
* @param User $ticketOwner
* @param User $user
* @param rmaTicket $rmaTicket
* @param Comment $comment
*
* @return method delivered()
*/
public function sendRmaTicketComments($ticketOwner, $user, RmaTicket $rmaTicket, $comment)
{
$this->to = $ticketOwner->email;
$this->subject = "RE[RMA Ticket # $rmaTicket->rma_ticket_id]";
$this->view = 'emails.rma.rma_comments';
$this->data = compact('ticketOwner', 'user', 'rmaTicket', 'comment');
return $this->deliver();
}
RmaComments模型
<?php
namespace App\Rma;
use Illuminate\Database\Eloquent\Model;
class RmaComments extends Model
{
protected $table = 'rma_comments';
protected $fillable = [
'rma_ticket_id', 'user_id', 'comment'
];
public function ticket()
{
return $this->belongsTo(RmaTicket::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
}
RMA模型
<?php
namespace App\Rma;
use Illuminate\Database\Eloquent\Model;
class RmaTicket extends Model
{
protected $table = 'rma_tickets';
protected $fillable = [
'user_id', 'rma_ticket_id', 'first_name', 'last_name',
'email', 'fax', 'company', 'marketplace_id', 'returnTypeId',
'product_name', 'quantity', 'order_number', 'address_one',
'address_two', 'city', 'state', 'zip_code', 'sku', 'model_number', 'comment',
'status_id', 'rma_number', 'refund_number', 'return_tracking_number',
'replacement_number', 'return_label'
];
public function returnType()
{
return $this->belongsTo(RmaReturnType::class);
}
public function marketplace()
{
return $this->belongsTo(RmaMarketplace::class);
}
public function status()
{
return $this->belongsTo(RmaStatus::class);
}
public function comments()
{
return $this->hasMany(RmaComments::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
}
答案 0 :(得分:1)
查看你的代码,如果你有dd($ comment-&gt; ticket)你就知道了,我相信这是你外键的命名。
your belongsTo:
deactivate
./bin/sshuttle mysshuser@mysshserver
需要ticket_id,但您将其命名为rma_ticket_id。
您可以在belongsTo关系中的第二个参数中指定不同的外键名称
public function ticket()
{
return $this->belongsTo(RmaTicket::class);
}
根据文档:
。 Eloquent通过检查关系方法的名称并使用_id为方法名称添加后缀来确定默认外键名称。但是,如果Phone模型上的外键不是user_id,则可以将自定义键名作为第二个参数传递给belongsTo方法: