ErrorException: 未定义的变量 laravel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45744062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:31:00  来源:igfitidea点击:

ErrorException: Undefined variable laravel

phplaravel

提问by Pain

I'm trying to pass variables from controller to view i tried both compact and with but nothing works, i get this error:

我正在尝试从控制器传递变量以查看我尝试了紧凑和有但没有任何效果,我收到此错误:

ErrorException undefined variable: programs.

ErrorException 未定义变量:程序。

Controller

控制器

public function getManageCourse(){
    $programs = Program::all();
    $academics = Academic::orderBy('academic_id','DESC')->get();
    return view(('courses.manageCourse'),compact('programs','academics'));
}

View

看法

<div class="col-sm-4">
    <label for="program">Course</label>
    <div class="input-group">
        <select class="form-control" name="program_id" id="program_id">
            <option value="">------------</option>
            @foreach($programs as $key =>$p)
                <option value="{{$p->$program_id}}">{{$y->program}}</option>
            @endforeach
        </select>
        <div class="input-group-addon">
            <span class="fa fa-plus" id="add-more-program"></span>
        </div>
    </div>
</div>

I have looked into similar problems but i didn't find a solution, why is this happening? what am i doing wrong?

我已经研究过类似的问题,但我没有找到解决方案,为什么会发生这种情况?我究竟做错了什么?

回答by Leo

make it like this:

让它像这样:

return view('courses.manageCourse',compact('programs','academics'));

not like you are doing it:

不像你这样做:

return view(('courses.manageCourse'),compact('programs','academics'));

回答by Pain

I found the solution i was doing $p->$program_idinstead of $p->program_id

我找到了我正在做的解决方案$p->$program_id而不是$p->program_id

回答by Shery

write

return view('courses.manageCourse',compact('programs','academics'));

and

<option value="{{$p->$program_id}}">{{$p->program}}</option>

instead of

代替

<option value="{{$p->$program_id}}">{{$y->program}}</option>