SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mkstudent.classes_student' 不存在 (SQL: 插入`classes_student` (`classes_id`,
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mkstudent.classes_student' doesn't exist (SQL: insert into `classes_student` (`classes_id`,
我不知道为什么它要存储在 mkstudent.classes_student table 中,我的意思是我创建了很多对很多 table class_student 并且当我托盘存储一个有 class 的学生正在发生此错误 谁能帮助我?
代码在这里
学生控制器
public function store(createstudentrequest $request)
{
$student= Student::create([
'first_name'=>$request->first_name,
'last_name'=>$request->last_name,
'phone_number'=>$request->phone_number
]);
if($request->class){
$student->classe()->attach($request->class);
}
session()->flash('success','student added successfully');
return redirect(route('students.index'));
}
student.create
<form action="{{route('students.store')}}" method="POST" enctype="multipart/form-data" >
@csrf
<div class="form-group">
<label for="title">First Name</label>
<input type="text" name="first_name" class="form-control" value="">
</div>
<div class="form-group">
<label for="title">Last Name</label>
<input type="text" name="last_name" class="form-control" value="">
</div>
<div class="form-group">
<label for="title">Phone Number</label>
<input type="text" name="phone_number" class="form-control" value="">
</div>
<div class="form-group">
<label for="Class">Class</label>
@if($classes->count() > 0)
<select name="class[]" id="class" multiple="true" class="form-control">
@foreach($classes as $class)
<option value="{{$class->id}}">
{{$class->name}}
</option>
@endforeach
</select>
@endif
</div>
class_student 的迁移是
public function up()
{
Schema::create('class_student', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('classes_id');
$table->integer('student_id');
$table->timestamps();
});
}
在学生模型中
class Student extends Model
{
protected $fillable = [
'first_name','last_name','phone_number'
];
public function classe()
{
return $this->belongsToMany(Classes::class);
}
}
在 classes 模型中
public function students()
{
return $this->belongsToMany(Student::class);
}
在关系中(在两个模型中)作为第二个参数,您必须提供 table 名称 class_student
因为您不遵循约定,因为您的模型名称是 Classes
您可以像这样在 'classe' 关系中将 table 名称作为第二个参数传递:
public function classe()
{
return $this->belongsToMany(Classes::class, 'class_student');
}
我不知道为什么它要存储在 mkstudent.classes_student table 中,我的意思是我创建了很多对很多 table class_student 并且当我托盘存储一个有 class 的学生正在发生此错误 谁能帮助我? 代码在这里 学生控制器
public function store(createstudentrequest $request)
{
$student= Student::create([
'first_name'=>$request->first_name,
'last_name'=>$request->last_name,
'phone_number'=>$request->phone_number
]);
if($request->class){
$student->classe()->attach($request->class);
}
session()->flash('success','student added successfully');
return redirect(route('students.index'));
}
student.create
<form action="{{route('students.store')}}" method="POST" enctype="multipart/form-data" >
@csrf
<div class="form-group">
<label for="title">First Name</label>
<input type="text" name="first_name" class="form-control" value="">
</div>
<div class="form-group">
<label for="title">Last Name</label>
<input type="text" name="last_name" class="form-control" value="">
</div>
<div class="form-group">
<label for="title">Phone Number</label>
<input type="text" name="phone_number" class="form-control" value="">
</div>
<div class="form-group">
<label for="Class">Class</label>
@if($classes->count() > 0)
<select name="class[]" id="class" multiple="true" class="form-control">
@foreach($classes as $class)
<option value="{{$class->id}}">
{{$class->name}}
</option>
@endforeach
</select>
@endif
</div>
class_student 的迁移是
public function up()
{
Schema::create('class_student', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('classes_id');
$table->integer('student_id');
$table->timestamps();
});
}
在学生模型中
class Student extends Model
{
protected $fillable = [
'first_name','last_name','phone_number'
];
public function classe()
{
return $this->belongsToMany(Classes::class);
}
}
在 classes 模型中
public function students()
{
return $this->belongsToMany(Student::class);
}
在关系中(在两个模型中)作为第二个参数,您必须提供 table 名称 class_student
因为您不遵循约定,因为您的模型名称是 Classes
您可以像这样在 'classe' 关系中将 table 名称作为第二个参数传递:
public function classe()
{
return $this->belongsToMany(Classes::class, 'class_student');
}