I'm trying to import a view to a file in pdf format and it says that I do not have the variable defined. I'm using laravel-dompdf and the migrations are in MySQL
.
I have this error:
(2/2) ErrorException
Undefined variable: cartItems (View: /var/www/html/meso/resources/views/cart/index.blade.php)
PDFController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Product;
use PDF;
class PDFController extends Controller
{
public function pdf(Request $request){
$products=Product::all();
view()->share('products', $products);
if ($request->has('descargar')) {
$pdf = PDF::loadView('cart.index');
return $pdf->download('cotizacion');
}
return view('cart.index');
}
}
My path in my file web
Route::get('/pdf', 'PDFController@pdf')->name('pdf');
My controller where he calls to my eyes that I want to import:
CartController
namespace App\Http\Controllers;
use App\Product;
use Gloudemans\Shoppingcart\Facades\Cart;
use Illuminate\Http\Request;
class CartController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$cartItems=Cart::content();
return view('cart.index',compact('cartItems'));
}
This is the view cart/index.blade.php
and it's where I call my route in a button at the end of the code:
@extends('layouts.main')
@section('content')
<div class="row">
<h3>Cart Items</h3>
<table class="table table-hover">
<thead>
<tr>
<th>Name</th>
<th>Price</th>
<th>qty</th>
<th>size</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach($cartItems as $cartItem)
<tr>
<td>{{$cartItem->name}}</td>
<td>{{$cartItem->price}}</td>
<td width="50px">
{!! Form::open(['route' => ['cart.update',$cartItem->rowId], 'method' => 'PUT']) !!}
<input name="qty" type="text" value="{{$cartItem->qty}}">
</td>
<td>
<div > {!! Form::select('size', ['small'=>'Small','medium'=>'Medium','large'=>'Large'] , $cartItem->options->has('size')?$cartItem->options->size:'' ) !!}
</div>
</td>
<td>
<input style="float: left" type="submit" class="button success small" value="Ok">
{!! Form::close() !!}
<form action="{{route('cart.destroy',$cartItem->rowId)}}" method="POST">
{{csrf_field()}}
{{method_field('DELETE')}}
<input class="button small alert" type="submit" value="Delete">
</form>
</td>
</tr>
@endforeach
<tr>
<td></td>
<td>
Tax: ${{Cart::tax()}} <br>
Sub Total: $ {{Cart::subtotal()}} <br>
Grand Total: $ {{Cart::total()}}
</td>
<td>Items: {{Cart::count()}}
</td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<a href="{{route('checkout.shipping')}}" class="button">Checkout</a>
<a href="{{route('pdf')}}" class="button small alert">Import to PDF</a>
</div>
@endsection