[英]how can i handle this error : Undefined offset: 0
I'm producing an online school platform and in part of it student can see it's classes with their informations..but the related function return this error : Undefined offset: 0 (View: C:\\xampp\\htdocs\\OnlineSchool\\resources\\views\\admin\\student\\ClassReport.blade.php)我正在制作一个在线学校平台,部分学生可以看到它的课程及其信息..但相关函数返回此错误:未定义偏移:0(视图:C:\\xampp\\htdocs\\OnlineSchool\\resources\\views \\admin\\student\\ClassReport.blade.php)
there are some relations between models My Models:模型之间有一些关系我的模型:
USER(students and teachers) ، LEVEL(level of classes) ، Classroom. USER(学生和教师)Ì LEVEL(班级)Ì课堂。
=> there is a many to many relation between student and classroom (pivot table) => 学生和课堂之间存在多对多关系(数据透视表)
please save me from this stupid error user:请让我远离这个愚蠢的错误用户:
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password','image','level','code_meli',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function classroom(){
return $this->hasMany (classroom::class);
}
public function ClassRoomStudent(){
return $this->belongsToMany (classroom::class ,'classroom_user','user_id','classroom_id');
}
}
classroom:课堂:
class classroom extends Model
{
protected $fillable = [
'title', 'teacher_id', 'level_id','day','time','price',
];
public function Student(){
return $this->belongsToMany (user::class ,'classroom_user','classroom_id','user_id');
}
public function level(){
return $this->belongsTo (level::class );
}
public function teacher(){
return $this->belongsTo (user::class );
}
}
level :等级 :
class level extends Model
{
protected $fillable = [
'title',
];
public function classroom(){
return $this->hasMany (classroom::class);
}
public function exam(){
return $this->hasMany (exam::class);
}
}
and my function in controller:和我在控制器中的功能:
public function MyClasses(){
$student_id=auth ()->user ()->id;
$classrooms=classroom::with ('level','teacher','factor')->wherehas('student',function ($q) use($student_id){
$q->where('id',$student_id);
})->get();
return view ('admin.student.ClassReport',compact ('classrooms','student_id'));
}
at the end .. my blade:最后..我的刀片:
@foreach($classrooms as $class)
<tr>
<td>{{$i++}}</td>
<td>{{$class->title}}</td>
<td>{{$class->teacher[0]->name}}</td>
.
.
.
.
</tr>
@endforeach
Classroom 只属于一位老师,不需要使用 de 零索引,只需输入 $class->teacher->name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.