未定义变量:$ Laravel 5.4

时间:2019-07-18 03:03:57

标签: php laravel

这是来自PemesananController的代码:

function index()
{
    $ruang = \App\ruangan::all();
    $sewa = \App\sewa::all();
    return view('/admin/pesan/index',compact('ruang', 'sewa'));
}

public function cetak_pdf()
{
    $sewa = \App\sewa::all();

    $pdf = PDF::loadview('',['sewa'=>$sewa]);
    return $pdf->download('laporan-guest-pdf')->stream();
}

这是我正在写给index.blade.php的代码:

@foreach($sewa as $r)

@endforeach

这是我的索引代码:

<a href="{{route('pdf')}}" class="btn btn-success my-3">EXPORT PDF</a>
<table class="table">
    <thead class=" text-primary">
        <th> No</th>
        <th> Nama tamu</th>
        <th> Ruangan</th>
        <th> CheckIn</th>
        <th> CheckOut</th>
        <th colspan="2"> Aksi</th>
    </thead>
    @foreach($sewa as $r)
        <tbody>
        <tr>
            <td>{{ $loop->iteration }}</td>
            <td>{{$r->nama_guest}}</td>
            <td> {{$r->ruangan}}</td>
            <td> {{$r->created_at}}</td>
            <td>{{ $r->tgl_checkout}}</td>
            <td>
                <a href="/admin/{{ $r->id }}/shows" class="btn btn-info btn-sm">lihat</a>
                <a href="/admin/{{ $r->id }}/edits" class="btn btn-warning btn-sm">Edit</a>
                <a href="/admin/{{ $r->id }}/haps" class="btn btn-danger btn-sm">Hapus</a>
                <a href="/admin/{{ $r->id }}/checkout" class="btn btn-primary btn-sm">Checkout</a>
            </td>
        </tr>
        <tr>
        </tbody>
    @endforeach
</table>

我得到这个:

Undefined variable: sewa (View:
C:\xampp\htdocs\Hotel_Lido\resources\views\admin\pesan\index.blade.php)

为什么会发生?一切正常,但是,当我单击导出PDF 按钮时,程序给我错误。

1 个答案:

答案 0 :(得分:0)

尝试

<?php

use App\Ruangan;
use App\Sewa;

class PemesananController extends Controller
{

    function index()
    {
        $ruang = Ruangan::all();
        $sewa = Sewa::all();

        return view('admin.pesan.index', compact('ruang', 'sewa'));
    }

}