简体   繁体   中英

DOMPDF - Laravel - Email PDF with Attachment

I am getting this error what would be the reason for this

"Undefined variable: quotation"

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');

    }

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

routes look like this

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 looks like this

@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

What am i missing here ? I am already passing $quotation to the edit view

Why are you using double brackets to declare a function ? Why not:

 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!'; } 

You are obviously not passing the $quotation variable through your route. You are also using the $quotation as an object; that tells me you don't intend passing it through the route. Try the following code:

public function update(Request $request, $quotation_id)
    {
      $quotation = Quotation::findOrFail($quotation_id);

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

     $info = ['info'=>$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');

       });
      echo 'Email was sent!';

    }

This should work.

I think you need to pass $quotation into the closure:

    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');

    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM