DOMPDF - Laravel - 通过附件发送电子邮件PDF?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (146)

我收到这个错误的原因是什么

“未定义的变量:引用”

QuotationController.php

public function update(Request $request, Quotation $quotation)
    {
      {


          $quotation->description= $request['description'];
          $quotation->qty= $request['qty'];
          $quotation->each_price= $request['each_price'];
          $quotation->save();

         $info = ['info'=>$quotation];

         Mail::send(['text'=>'mail'], $info, function($message){

             $pdf = PDF::loadView('employees.quotations.edit', $quotation);

             $message->to('example@gmail.com','John Doe')->subject('Quotation');

             $message->from('from@gmail.com','The Sender');

             $message->attachData($pdf->output(), 'filename.pdf');

           });
          echo 'Email was sent!';

        }
      }

public function edit(Quotation $quotation)
    {
        return view('employees.quotations.edit', compact('quotation'));
        //return view('employees.quotations.edit')->with('quotation');

    }

......................................................................

路线看起来像这样

Route::post('/quotation', 'Employee\QuotationController@store')->name('employee.quotation.store');
  Route::get('/quotation', 'Employee\QuotationController@index')->name('employee.quotation.index');
  Route::get('/quotation/create', 'Employee\QuotationController@create')->name('employee.quotation.create');
  Route::put('/quotation/{quotation}', 'Employee\QuotationController@update')->name('employee.quotation.update');
  Route::get('/quotation/{quotation}', 'Employee\QuotationController@show')->name('employee.quotation.show');
  Route::delete('/quotation/{quotation}', 'Employee\QuotationController@destroy')->name('employee.quotation.destroy');
  Route::get('/quotation/{quotation}/edit', 'Employee\QuotationController@edit')->name('employee.quotation.edit');

employees.quotations.edit.blade.php看起来像这样

@section('left-menu')

@endsection

@section('right-menu')

@endsection

@section('content')
  <h1>Update a Quotation</h1>
  <br><br>

    <form action="{{ route('employee.quotation.update',$quotation->id) }}" method="post">
      @method('PUT')
      @csrf
      <div class="form-group">
        <label for="inputJobDescription">Description</label>
        <textarea class="form-control" rows="2" id="inputQuoteDescription" name="description" placeholder="Description">{{$quotation->description}}
</textarea>
      </div>
      <div class="form-group row">
        <label for="inputQty" class="col-2 col-form-label">Qty</label>
        <div class="col-10">
          <input type="text" class="form-control" id="inputQty" name="qty" value="{{$quotation->qty}}" oninput="quotation_calculate()" onchange="quotation_calculate()">
        </div>
      </div>
      <div class="form-group row">
        <label for="inputEachPrice" class="col-2 col-form-label">Each Price</label>
        <div class="col-10">
          <input type="text" class="form-control" id="inputEachPrice" name="each_price" value="{{$quotation->each_price}}" oninput="quotation_calculate()" onchange="quotation_calculate()">
        </div>
      </div>
      <button type="submit" class="btn btn-primary">Submit</button>
    </form>
@endsection

@section('pagescript')

@stop

我在这里缺少什么?我已将$ quotation传递给编辑视图

提问于
用户回答回答于

你显然没有$quotation通过你的路线传递变量。你也在使用它$quotation作为一个对象; 告诉我你不打算通过这条路线。请尝试以下代码:

public function update(Request $request)
    {
      $quotation = new Quotation;

      $quotation->description= $request['description'];
      $quotation->qty= $request['qty'];
      $quotation->each_price= $request['each_price'];
      $quotation->save();

     $info = ['info'=>$quotation];

     Mail::send(['text'=>'mail'], $info, function($message){

         $pdf = PDF::loadView('employees.quotations.edit', $quotation);

         $message->to('example@gmail.com','John Doe')->subject('Quotation');

         $message->from('from@gmail.com','The Sender');

         $message->attachData($pdf->output(), 'filename.pdf');

       });
      echo 'Email was sent!';

    }

这应该工作。

用户回答回答于

我认为你需要$quotation进入关闭:

    Mail::send(['text' => 'mail'], $info, function ($message) use ($quotation) {

        $pdf = PDF::loadView('employees.quotations.edit', $quotation);

        $message->to('example@gmail.com', 'John Doe')->subject('Quotation');

        $message->from('from@gmail.com', 'The Sender');

        $message->attachData($pdf->output(), 'filename.pdf');

    });

扫码关注云+社区

领取腾讯云代金券

年度创作总结 领取年终奖励