gpt4 book ai didi

php - 在 mailable/markdown Laravel 中传递变量

转载 作者:行者123 更新时间:2023-12-02 22:36:01 25 4
gpt4 key购买 nike

我正在尝试将这张票证收集的变量发送到我在 Laravel 中的 markdown 电子邮件。

enter image description here

我收到此错误

"Trying to get property of non-object" and cannot figure out how to pass all the variables into my markdown email.

以下是我正在采取的步骤:

<小时/>

路线:

Route::post('/admin', 'AdminController@emailOpenStoreTicket')->name('email-open-store');

收集工单数据的表单:

<form action="{{route('email-open-store')}}" method="POST" enctype="multipart/form-data">
@csrf
{{method_field('post')}}
<input type="email" name="staff_email" required>
<textarea name="rec_message" required></textarea>
<input type="hidden" name="subject" value="{{ $ot->subject}}">
...etc
<input type="hidden" name="street" value="{{ $ot->street }}">
<button type="submit">Send</button>
</form>

管理 Controller :

public function emailOpenStoreTicket(Request $request)
{
$validatedData = $request->validate([
'staff_email' => 'required|email',
'rec_message' => 'required|min:2'
]);
$ticket = new EmailOpenTicket;
$ticket->staff_email=$request->staff_email;
$ticket->rec_message=$request->rec_message;
$ticket->subject=Purifier::clean($request->subject);
$ticket->content=Purifier::clean($request->content);
$ticket->username=$request->username;
$ticket->user_id=$request->user_id;
$ticket->street=$request->street;
$ticket->first_name=$request->first_name;
$ticket->last_name=$request->last_name;
$ticket->email=$request->email;
$ticket->file_name=$request->file_name;

Mail::to($ticket->staff_email)->send(new EmailOpenTicket);
return back()->with('success','Your email message has been sent.');
}

邮件类如果我使用:

  public function __construct(Ticket $ticket)
{
$this->ticket = $ticket;
}

出现以下错误:

Type error: Too few arguments to function App\Mail\EmailOpenTicket::__construct(), 0 passed in app\Http\Controllers\AdminController.php on line 71 and exactly 1 expected

上面的管理 Controller 中的这一行是:$ticket = new EmailOpenTicket;

namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Ticket;
use App\Admin;

class EmailOpenTicket extends Mailable
{
use Queueable, SerializesModels;
public $ticket;
/**
* Create a new message instance.
*
* @return void
*/
// public function __construct(Ticket $ticket)
// {
// $this->ticket = $ticket;
// }

/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->subject('New Open Maintenance Ticket')->markdown('admin.tickets.emailopenticket');
}
}

然后是我的电子邮件 View :

我读到,如果我添加 ['ticket' => $ticket] 它将有助于引入变量...在这种情况下,它会跳过 ['ticket' => $ticket] 并在下一行出现错误:

Trying to get property of non-object

@component('mail::message', ['ticket' => $ticket])
#{{$ticket->rec_message}}

{{$ticket->first_name}} {{$ticket->last_name}} has submitted a new maintenance ticket.
Please review and complete as soon as possible

@component('mail::panel')
**Details:**
- **Ticket Type:** Maintenance Request
- **Ticket Number:** {{$ticket->st_id}}
- **Subject:** {{$ticket->subject}}
..etc

我已经这样做了两天了。如果有人能伸出援手,我将不胜感激

最佳答案

如果您的 EmailOpenTicket 类的构造函数定义了必需的参数,则需要在实例化该类时将其传入。

// Assuming $ticket is already defined as a Ticket object:
$email = new EmailOpenTicket($ticket);

此外,当您发送 Mailable 时,请确保传入您已创建并向其分配数据的实例。在您的代码示例中,您正在实例化第二个 EmailOpenTicket 类。

Mail::to($ticket->staff_email)->send($ticket);
<小时/>

提供一些额外的建议:似乎 Ticket 已经是一个数据库中包含数据的模型。如果是这种情况,您实际上不需要通过表单发送所有数据。您所需要的只是您要发送的票证的 ID。然后,您的 Controller 可以找到该票证,将其附加到可邮寄的邮件中,然后就可以上路了。

表格:

<form action="{{route('email-open-store')}}" method="POST" enctype="multipart/form-data">
@csrf
{{method_field('post')}}
<input type="email" name="staff_email" required>
<textarea name="rec_message" required></textarea>
<input type="hidden" name="ticket_id" value="{{ $ot->id }}">
<button type="submit">Send</button>
</form>

Controller :

public function emailOpenStoreTicket(Request $request)
{
$validatedData = $request->validate([
'staff_email' => 'required|email',
'rec_message' => 'required|min:2',
// Validate the ticket ID exists in the DB
'ticket_id' => 'required|exists:tickets,id'
]);

// Grab the Ticket via Eloquent
$ticket = Ticket::findOrFail($request->ticket_id);

// Create the Mailable, passing in the Ticket
$email = new EmailOpenTicket($ticket);
$email->staff_email = $request->staff_email;
$email->rec_message = $request->rec_message;

Mail::to($email->staff_email)->send($email);

return back()->with('success','Your email message has been sent.');
}

可邮寄:

namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Ticket;
use App\Admin;

class EmailOpenTicket extends Mailable
{
use Queueable, SerializesModels;

public $ticket;
public $staff_email;
public $rec_message;

/**
* Create a new message instance.
*
* @return void
*/
public function __construct(Ticket $ticket)
{
$this->ticket = $ticket;
}

/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->subject('New Open Maintenance Ticket')->markdown('admin.tickets.emailopenticket');
}
}

关于php - 在 mailable/markdown Laravel 中传递变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49836216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com