how to fix Undefined variable: collaborators in do

2019-03-04 17:09发布

问题:

This question already has an answer here:

  • “Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHP 27 answers

I am going to print PDF using domPDF. this is My CollaboPDFController.php:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use PDF;
use App\Collaboration;

class CollaboPDFController extends Controller
{
    public function getPDF(){
        $collaborators = Collaboration::all();
        $pdf = PDF::loadView('collaborators.form',['Collaborators'=>$collaborators]);
        return $pdf->stream('form.collaborators');
    }
    //
}

and this is My blade view witch I am going to print as pdf file. it is form.blade.php file in collaborators folder.

<div class="col-md-4" style="border:1px solid #ccc;margin-left:15px;padding:10px;">
        <h4 class="page-header">
            Collaborators
        </h4>
        @if($collaborators)
           @foreach( $collaborators as $collaborator)
                <div>
                    <div>
                        <span>
                            <img src="{{ $collaborator->user()->first()->getAvatarUrl() }}" />
                             {{ $collaborator->user()->first()->username}}
                             {{ $collaborator->user()->first()->id}}

                        </span>
                    </div> 

回答1:

As far as I know, variables are case-sensitive, try to replace ['Collaborators'=>$collaborators] to ['collaborators'=>$collaborators]