在Laravel中传递控制器和可邮寄之间的数据

时间:2017-11-30 16:49:38

标签: laravel email

在我将模型记录的创建形式提交给mailable后,我似乎无法传输数据。我认为我的大部分内容都是正确的,但我仍然坚持通过控制器和我的mailable之间的间隙传递数据。

我有一个具有以下功能的货运控制器:

npm run dev

现在您可以看到底部附近有一条线" Mail :: to($ user) - > send(new newFreightBill($ shipment));",我想要的是什么正在做的是将数据从提交的$货件复制到mailable

我的newFreightBill可邮寄:

public function store(Request $request)
{

    $this->validate(request(), [

        'pro_number' => 'required',
        'shipment_origin' => 'required'
        /*'piecesNumber' => 'required' (had to remove for now, but must review)*/

    ]);

    $user_id = Auth::id();

    $input = $request->all();

    //Save Initial Shipment Data
    $shipment = new Shipment();
    $shipment->pro_number = request('pro_number');
    $shipment->shipment_origin = request('shipment_origin');
    $shipment->date = request('date');
    $shipment->due_date = request('due_date');
    $shipment->tractor_id = request('tractor_id');
    $shipment->trailer_id = request('trailer_id');
    $shipment->driver_id = request('driver_id');
    $shipment->notes = request('notes');
    $shipment->shipper_no = request('shipper_no');
    $shipment->ship_to = request('ship_to');
    $shipment->ship_from = request('ship_from');
    $shipment->bill_to = request('bill_to');
    $shipment->bill_type = request('bill_type');
    $shipment->load_date = request('load_date');
    $shipment->shipment_status = 0;
    $shipment->created_by = $user_id;

    $shipment->save();

    //Save Shipment Details

    for ($i = 0; $i < count($request->shipment_details['piecesNumber']); $i++) {

        Shipment_Detail::create([
            'shipment_id' => $shipment->id,
            'pieces_number' => $request->shipment_details['piecesNumber'][$i],
            'pieces_type' => $request->shipment_details['piecesType'][$i],
            'rate_type' => $request->shipment_details['rateType'][$i],
            'charge' => $request->shipment_details['charge'][$i],
            'weight' => $request->shipment_details['weight'][$i],
            'hazmat' => $request->shipment_details['hazmat'][$i],
            'description' => $request->shipment_details['description'][$i] ]);
    }
    $user = Auth::user()->email;

    Mail::to($user)->send(new newFreightBill($shipment));

  Session::flash('success_message','Freight Bill Successfully Created'); //<--FLASH MESSAGE

    //Return to Register
  return redirect('/shipments/i/'.$shipment->url_string);

}

和我的邮件模板(我在测试中收到的):

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

use App\Shipment;

class newFreightBill extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public $shipment;

    public function __construct(Shipment $shipment)
    {
        $shipment = $this->shipment;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from('MYEMAIL')
                    ->view('emails.shipments.created')->with(['shipment', $this->shipment]);
    }
}

正如您所看到的,模板至少可以说是非常简单。我收到以下电子邮件给我:

  

专业号码:

但绝对没有别的,没有实际的数字。所以我知道问题是将表单提交中的数据传递给可邮寄变量。

1 个答案:

答案 0 :(得分:1)

如果您使用的是Mail类的属性,例如public $shipment,则不应使用with()方法。您无需使用with即可在您的视图中使用您的课程属性。

然后你应该这样做:专业号码:{{ $shipment->pro_number }}

哦,是的,而且:$shipment = $this->shipment;是向后的,它应该是$this->shipment = $shipment;