我收到错误"未定义的变量:考试(查看:C:\ xampp \ htdocs \ myproject01 \ resources \ views \ home.blade.php)"当我运行我的laravel代码时,如下所示(控制器名称为ExamController.php):
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Exam;
class ExamController extends Controller
{
public function index(){
$exams = Exam::all();
return view('home',compact('exams'));
}
}
以下是名为Exam.php的模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Exam extends Model
{
protected $table = 'exams';
}
这是我的观点,位于views / home,名为&#34; home.blade.php&#34;:
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Dashboard</div>
<div class="panel-body">
<form method='POST' action='/posts'>
{{ csrf_field() }}
<div class="form-group">
<label for="">Exams Name</label>
<select name="exam" id="exam" class="form-control input-sm depertment">
<option value="" id="exam_slect">Select Exams</option>
@foreach($exams as $exam)
<option value="{{$exam->id}}">{{$exam->name}}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="">Depertment</label>
<select name="depertment" id="depertment" class="form-control input-sm depertment">
<option value="" id="dept_slect">Select Depertment</option>
@foreach($depertments as $depertment)
<option value="{{$depertment->id}}">{{$depertment->name}}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="">Teacher</label>
<select name="teacher" id="teacher" class="form-control input-sm teacher">
<option value="#dept_slect">Select Teacher</option>
</select>
</div>
<div class="form-group">
<label for="start_date">Date</label>
<input type="date" class="form-control" name="start_date" id="start_date" placeholder="mm/dd/yyyy">
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Publish"></input>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
@section('script')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$(".depertment").change(function() {
console.log($(".depertment").val());
$.getJSON("/home/teacher/" + $(".depertment").val(), function(data) {
var $teachers = $(".teacher");
$teachers.empty();
$.each(data, function(id, name) {
$teachers.append('<option value="' + id +'">' + name + '</option>');
});
$(".teacher").trigger("change"); /* trigger next drop down list not in the example */
});
});
});
</script>
@endsection
我使用此http://localhost:8000/home ***从我的浏览器调用代码 Output
这是我的错误:
Whoops, looks like something went wrong.
2/2
ErrorException in 6da755f502f5377537b5d00f9f88cb42e31cc6de.php line 20:
Undefined variable: exams (View: C:\xampp\htdocs\myproject01\resources\views\home.blade.php)
in 6da755f502f5377537b5d00f9f88cb42e31cc6de.php line 20
at CompilerEngine->handleViewException(object(ErrorException), 1) in PhpEngine.php line 44
at PhpEngine->evaluatePath('C:\\xampp\\htdocs\\myproject01\\storage\\framework\\views/6da755f502f5377537b5d00f9f88cb42e31cc6de.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'depertments' => object(Collection))) in CompilerEngine.php line 59
at CompilerEngine->get('C:\\xampp\\htdocs\\myproject01\\resources\\views/home.blade.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'depertments' => object(Collection))) in View.php line 137
at View->getContents() in View.php line 120
at View->renderContents() in View.php line 85
at View->render() in Response.php line 38
at Response->setContent(object(View)) in Response.php line 225
at Response->__construct(object(View)) in Router.php line 602
at Router->prepareResponse(object(Request), object(View)) in Router.php line 559
at Router->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 30
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in SubstituteBindings.php line 41
at SubstituteBindings->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Authenticate.php line 43
at Authenticate->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 65
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Router.php line 561
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 520
at Router->dispatchToRoute(object(Request)) in Router.php line 498
at Router->dispatch(object(Request)) in Kernel.php line 174
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 30
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in TransformsRequest.php line 30
at TransformsRequest->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in TransformsRequest.php line 30
at TransformsRequest->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ValidatePostSize.php line 27
at ValidatePostSize->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Kernel.php line 149
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 116
at Kernel->handle(object(Request)) in index.php line 53
at require_once('C:\\xampp\\htdocs\\myproject01\\public\\index.php') in server.php line 21
1/2
ErrorException in 6da755f502f5377537b5d00f9f88cb42e31cc6de.php line 20:
Undefined variable: exams
in 6da755f502f5377537b5d00f9f88cb42e31cc6de.php line 20
at HandleExceptions->handleError(8, 'Undefined variable: exams', 'C:\\xampp\\htdocs\\myproject01\\storage\\framework\\views\\6da755f502f5377537b5d00f9f88cb42e31cc6de.php', 20, array('__path' => 'C:\\xampp\\htdocs\\myproject01\\storage\\framework\\views/6da755f502f5377537b5d00f9f88cb42e31cc6de.php', '__data' => array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'depertments' => object(Collection)), 'obLevel' => 1, '__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'depertments' => object(Collection))) in 6da755f502f5377537b5d00f9f88cb42e31cc6de.php line 20
at include('C:\\xampp\\htdocs\\myproject01\\storage\\framework\\views\\6da755f502f5377537b5d00f9f88cb42e31cc6de.php') in PhpEngine.php line 42
at PhpEngine->evaluatePath('C:\\xampp\\htdocs\\myproject01\\storage\\framework\\views/6da755f502f5377537b5d00f9f88cb42e31cc6de.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'depertments' => object(Collection))) in CompilerEngine.php line 59
at CompilerEngine->get('C:\\xampp\\htdocs\\myproject01\\resources\\views/home.blade.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'depertments' => object(Collection))) in View.php line 137
at View->getContents() in View.php line 120
at View->renderContents() in View.php line 85
at View->render() in Response.php line 38
at Response->setContent(object(View)) in Response.php line 225
at Response->__construct(object(View)) in Router.php line 602
at Router->prepareResponse(object(Request), object(View)) in Router.php line 559
at Router->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 30
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in SubstituteBindings.php line 41
at SubstituteBindings->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Authenticate.php line 43
at Authenticate->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 65
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Router.php line 561
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 520
at Router->dispatchToRoute(object(Request)) in Router.php line 498
at Router->dispatch(object(Request)) in Kernel.php line 174
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 30
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in TransformsRequest.php line 30
at TransformsRequest->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in TransformsRequest.php line 30
at TransformsRequest->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ValidatePostSize.php line 27
at ValidatePostSize->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 102
at Pipeline->then(object(Closure)) in Kernel.php line 149
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 116
at Kernel->handle(object(Request)) in index.php line 53
at require_once('C:\\xampp\\htdocs\\myproject01\\public\\index.php') in server.php line 21