0

再会

提交表单后,我正在使用 Mailgun 向用户发送包含订单所需信息的电子邮件,我设法发送了主题和电子邮件地址,但我无法发送在创建时分配的随机数命令。

这是我的控制器:

public function store(Request $request)
{
    $order = $user->orders()->create([

            'randomid' => rand(100000,999999),
            'subject' => $request->get('subject'),
            'email' => $request->get('email'),
    ]);

    $data = $request->only('subject', 'email', 'randomid');

    Mail::send('emails.note',
        $data
        , function($message) use ($data)
        {
            $message->subject('New Order: '.$data['subject'])
                ->from('myemail@myserver.com')
                ->to($data['email']);
        });
}
4

2 回答 2

0

我看到您从请求对象中获取了 $data

$data = $request->only('subject', 'email', 'randomid');

但 randomid 是在 created 方法中生成的

    $order = $user->orders()->create([

        'randomid' => rand(100000,999999),
        'subject' => $request->get('subject'),
        'email' => $request->get('email'),
]);

所以没有 randomid要求。

我认为您应该从 $order 获取 $data ,如下所示:

$data = $order->toArray();

所以你将拥有:

public function store(Request $request)
{
    $order = $user->orders()->create([

            'randomid' => rand(100000,999999),
            'subject' => $request->get('subject'),
            'email' => $request->get('email'),
    ]);

    $data = $order->toArray();

    Mail::send('emails.note',
        $data
        , function($message) use ($data)
        {
            $message->subject('New Order: '.$data['subject'])
                ->from('myemail@myserver.com')
                ->to($data['email']);
        });
}
于 2016-07-12T09:15:17.603 回答
0

randomid不是来自请求。rand(100000,999999)您使用!手动生成它的值

请试试这个:

public function store(Request $request){
    $data = [
        'randomid' => rand(100000, 999999) , 
        'subject' => $request->input('subject') , 
        'email' => $request->input('email')
    ];
    $order = $user->orders()->create($data);
    Mail::send('emails.note', $data, function ($message) use($data) {
        $message->subject('New Order: ' . $data['subject'])
        ->from('myemail@myserver.com')->to($data['email']);
    });
}
于 2016-07-12T09:11:05.633 回答