我不知道如何从这个外键中获取数据。我已经遵循了文档中的所有步骤,但我仍然不知道需要做什么。这是我的老师表(主要部分功能向上(:
public function up()
{
Schema::create('teachers', function (Blueprint $table) {
$table->id();
$table->unsignedbiginteger('levels_id');
$table->foreign('levels_id')->references('id')->on('levels');
$table->string('teacher_name');
$table->string('teacher_email')->unique();
$table->string('teacher_home_phone');
$table->string('teacher_mobile_phone');
$table->string('teacher_work_phone');
$table->string('teacher_home_address');
$table->string('teacher_suburb');
$table->string('teacher_postcode');
$table->string('teacher_username');
$table->string('teacher_password');
$table->timestamps();
});
}
这是我的水平表:
public function up()
{
Schema::create('levels', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('levels_name');
$table->timestamps();
});
}
这是我的型号Teacher.php
:
class Teacher extends Models {
protected $fillable = [
'levels_id',
'teacher_name',
'teacher_email',
'teacher_home_phone',
'teacher_work_phone',
'teacher_mobile_phone',
'teacher_home_address',
'teacher_suburb',
'teacher_postcode',
'teacher_username',
'teacher_password',
'teacher_level',
];
public function levels()
{
return $this->hasMany(Levels::class);
}
}
这是我的levels.php
文件:
class Levels extends Model
{
use HasFactory;
protected $fillable = [
'levels_id',
'levels_name',
];
public function levels()
{
return $this->belongsTo(Teacher::class);
}
}
这是一个适用于教师\partils\form.blade.hp:的刀片文件
<div class="form-group col-md-5">
<label for="Teacher_Name" class="required">Mention levels Instructor's going to teach </label>
<div class="col-md-12">
<div class="form-check form-check-inline col-md-3">
<input class="form-check-input" type="checkbox" name="levels_id[]" value="Level 1" @if(isset($teacher->levels_id) && @in_array('Level 1', @$teacher->levels_id)) {{'checked'}} @endif>
<label class="form-check-label">Level 1</label>
</div>
<div class="form-check form-check-inline col-md-3">
<input class="form-check-input" type="checkbox" name="levels_id[]" value="Level 2" @if(isset($teacher->levels_id) && @in_array('Level 2', @$teacher->levels_id)) {{'checked'}} @endif>
<label class="form-check-label">Level 2</label>
</div>
<div class="form-check form-check-inline col-md-3">
<input class="form-check-input" type="checkbox" name="levels_id[]" value="Level 3" @if(isset($teacher->levels_id) && @in_array('Level 3', @$teacher->levels_id)) {{'checked'}} @endif>
<label class="form-check-label">Level 3</label>
</div>
<div class="form-check form-check-inline col-md-3">
<input class="form-check-input" type="checkbox" name="levels_id[]" value="Level 4" @if(isset($teacher->levels_id) && @in_array('Level 4', @$teacher->levels_id)) {{'checked'}} @endif>
<label class="form-check-label">Level 4</label>
</div>
<div class="form-check form-check-inline col-md-3">
<input class="form-check-input" type="checkbox" name="levels_id[]" value="Level 5" @if(isset($teacher->levels_id) && @in_array('Level 5', @$teacher->levels_id)) {{'checked'}} @endif>
<label class="form-check-label">Level 5</label>
</div>
<div class="form-check form-check-inline col-md-3">
<input class="form-check-input" type="checkbox" name="levels_id[]" value="Level 6" @if(isset($teacher->levels_id) && @in_array('Level 6', @$teacher->levels_id)) {{'checked'}} @endif>
<label class="form-check-label">Level 6</label>
</div>
<div class="form-check form-check-inline col-md-3">
<input class="form-check-input" type="checkbox" name="levels_id[]" value="Level 7" @if(isset($teacher->levels_id) && @in_array('Level 7', @$teacher->levels_id)) {{'checked'}} @endif>
<label class="form-check-label">Level 7</label>
</div>
<div class="form-check form-check-inline col-md-3">
<input class="form-check-input" type="checkbox" name="levels_id[]" value="Level 8" @if(isset($teacher->levels_id) && @in_array('Level 8', @$teacher->levels_id)) {{'checked'}} @endif>
<label class="form-check-label">Level 8</label>
</div>
<div class="form-check form-check-inline col-md-3">
<input class="form-check-input" type="checkbox" name="levels_id[]" value="Level 9" @if(isset($teacher->levels_id) && @in_array('Level 9', @$teacher->levels_id)) {{'checked'}} @endif>
<label class="form-check-label">Level 9</label>
</div>
<div class="form-check form-check-inline col-md-3">
<input class="form-check-input" type="checkbox" name="levels_id[]" value="Level 10" @if(isset($teacher->levels_id) && @in_array('Level 10', @$teacher->levels_id)) {{'checked'}} @endif>
<label class="form-check-label">Level 10</label>
</div>
<div class="form-check form-check-inline col-md-3">
<input class="form-check-input" type="checkbox" name="levels_id[]" value="Level 11" @if(isset($teacher->levels_id) && @in_array('Level 11', @$teacher->levels_id)) {{'checked'}} @endif>
<label class="form-check-label">Level 11</label>
</div>
<div class="form-check form-check-inline col-md-3">
<input class="form-check-input" type="checkbox" name="levels_id[]" value="Level 12" @if(isset($teacher->levels_id) && @in_array('Level 12', @$teacher->levels_id)) {{'checked'}} @endif>
<label class="form-check-label">Level 12</label>
</div>
<div class="form-check form-check-inline col-md-4">
<input class="form-check-input" type="checkbox" name="levels_id[]" value="undergraduate" @if(isset($teacher->levels_id) && @in_array('undergraduate', @$teacher->levels_id)) {{'checked'}} @endif>
<label class="form-check-label">Undergraduate</label>
</div>
<div class="form-check form-check-inline col-md-4">
<input class="form-check-input" type="checkbox" name="levels_id[]" value="postgraduate" @if(isset($teacher->levels_id) && @in_array('postgraduate', @$teacher->levels_id)) {{'checked'}} @endif>
<label class="form-check-label">Postgraduate</label>
</div>
您在模型中使用了错误的关系,您的表结构定义您应该在此处使用belongsTo-Rlelation。
public function levels()
{
return $this->belongsTo(Levels::class);
}
关系则相反(根据表结构(。一个老师属于一个级别。
如果是,您应该使用:
public function level()
{
return $this->belongsTo(Levels::class);
}
此外,用单数命名关系更有意义,因为它只属于一个级别,而不属于多个级别(您应该对您的模型也这样做(。
对于另一种方式(要从$level
实例访问教师模型,可以从Levels模型定义Has Many关系:
public function teachers()
{
return $this->hasMany(Teacher::class);
}
有关更多信息,请查看相关文档。