Laravel - 未定义变量:讲师
Laravel - Undefined variable: Instructor
我正在尝试通过 create.blade.php 上的表单传递具有特定 role_id 的用户,见下文;
@extends('layouts.app')
@section('content')
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Create Course</div>
<div class="card-body">
<form method="POST" action="{{ route('admin.courses.store') }}" enctype="multipart/form-data">
@csrf
<div class="panel-body">
@if (Auth::user()->isAdmin())
<div class="row">
<div class="col-xs-12 form-group">
{!! Form::label('Instructor', 'Instructor', ['class' => 'control-label']) !!}
{!! Form::select('Instructor[]', $Instructor, old('Instructor'), ['class' => 'form-control select2', 'multiple' => 'multiple']) !!}
<p class="help-block"></p>
@if($errors->has('Instructor'))
<p class="help-block">
{{ $errors->first('Instructor') }}
</p>
@endif
</div>
</div>
@endif
<div class="form-group">
<label class="required" for="name">Course Title</label>
<input class="form-control {{ $errors->has('title') ? 'is-invalid' : '' }}" type="text" name="title" id="id" value="{{ old('title', '') }}" required>
@if($errors->has('name'))
<div class="invalid-feedback">
{{ $errors->first('name') }}
</div>
@endif
</div>
<div class="form-group">
<button class="btn btn-danger" type="submit">
Save
</button>
</div>
</form>
</div>
</div>
@endsection
User.php
class User extends Authenticatable
{
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
public function role()
{
return $this->belongsToMany(Role::class, 'role_user');
}
public function isAdmin()
{
return $this->role()->where('role_id', 1)->first();
}
public function roles(){
return $this->belongsToMany('App\Role');
}
CoursesController.php
我的创建函数:
protected function create()
{
{
$Instructor = \App\User::whereHas('role', function ($q) { $q->where('role_id', 2); } )->get()->pluck('title', 'id');
// $courses = Course::all()->pluck('title');
return view('admin.courses.create', compact('courses'));
}
}
返回以下错误:
Undefined variable: Instructor (View:
C:\xampp\htdocs\test\resources\views\admin\courses\create.blade.php)
我不确定我哪里做错了。感谢您的帮助。
参考我最近的评论:
在控制器中,您缺少将 Instructor 传递给您的视图:
return view('admin.courses.create', compact('courses', 'Instructor'));
我正在尝试通过 create.blade.php 上的表单传递具有特定 role_id 的用户,见下文;
@extends('layouts.app')
@section('content')
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Create Course</div>
<div class="card-body">
<form method="POST" action="{{ route('admin.courses.store') }}" enctype="multipart/form-data">
@csrf
<div class="panel-body">
@if (Auth::user()->isAdmin())
<div class="row">
<div class="col-xs-12 form-group">
{!! Form::label('Instructor', 'Instructor', ['class' => 'control-label']) !!}
{!! Form::select('Instructor[]', $Instructor, old('Instructor'), ['class' => 'form-control select2', 'multiple' => 'multiple']) !!}
<p class="help-block"></p>
@if($errors->has('Instructor'))
<p class="help-block">
{{ $errors->first('Instructor') }}
</p>
@endif
</div>
</div>
@endif
<div class="form-group">
<label class="required" for="name">Course Title</label>
<input class="form-control {{ $errors->has('title') ? 'is-invalid' : '' }}" type="text" name="title" id="id" value="{{ old('title', '') }}" required>
@if($errors->has('name'))
<div class="invalid-feedback">
{{ $errors->first('name') }}
</div>
@endif
</div>
<div class="form-group">
<button class="btn btn-danger" type="submit">
Save
</button>
</div>
</form>
</div>
</div>
@endsection
User.php
class User extends Authenticatable
{
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
public function role()
{
return $this->belongsToMany(Role::class, 'role_user');
}
public function isAdmin()
{
return $this->role()->where('role_id', 1)->first();
}
public function roles(){
return $this->belongsToMany('App\Role');
}
CoursesController.php
我的创建函数:
protected function create()
{
{
$Instructor = \App\User::whereHas('role', function ($q) { $q->where('role_id', 2); } )->get()->pluck('title', 'id');
// $courses = Course::all()->pluck('title');
return view('admin.courses.create', compact('courses'));
}
}
返回以下错误:
Undefined variable: Instructor (View: C:\xampp\htdocs\test\resources\views\admin\courses\create.blade.php)
我不确定我哪里做错了。感谢您的帮助。
参考我最近的评论:
在控制器中,您缺少将 Instructor 传递给您的视图:
return view('admin.courses.create', compact('courses', 'Instructor'));