這是我的課程模型
class Classes extends Model
{
protected $fillable = [
'name','course_catagory_id','teacher_id'
];
public function coursecatagory()
{
return $this->belongsTo(CourseCatagory::class);
}
public function teacher()
{
return $this->belongsToMany(Teacher::class);
}
}
和遷移
public function up()
{
Schema::create('classes', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->integer('course_catagory_id');
$table->integer('teacher_id');
$table->timestamps();
});
}
并創建課程頁面
<div class="card-body">
@if(session()->has('success'))
<div class="alert alert-success">
{{session()->get('success')}}
</div>
@endif
<form method="POST" action="{{ route('courses.store')}}">
@csrf
<table class="table table-boreder">
<tr>
<th>Name</th>
<td>
<input type="text" name="name" class="form-control">
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="submit" class="btn btn-primary ">
</td>
</tr>
</table>
</form>
</div>
</div>
和類索引頁面
<div class="card-body">
@if($data->count()>0)
<table class="table">
<thead>
<th>name</th>
<th>Course Catagory</th>
<th>
Teacher
</th>
</thead>
<tbody>
@foreach($data as $d)
<tr>
<td>
{{$d->name}}
</td>
<td>
{{$d->coursecatagory['name']}}
</td>
<td>
<a href="#">
{{$d->teacher['first_name']}}
</a>
</td>
</tr>
@endforeach
</tbody>
</table>
@else
<h3 class="text-center">No class Yet!</h3>
@endif
</div>
當我創建一個新類時,它保存在資料庫中,但不限于索引視圖,當我托盤查看索引視圖時,只顯示類名,其他欄位不顯示,錯誤面臨錯誤
uj5u.com熱心網友回復:
如果您使用$table受保護的屬性更新您的 Classes 模型會發生什么:
protected $table = 'classes_teacher'
或者,無論表名應該是什么?
uj5u.com熱心網友回復:
'mkstudent.classes_teacher' 不存在 (SQL: select
teachers.*,classes_teacher.`cla ...
問題是 classes_teacher 不是復數。我應該被命名classes_teachers。但是您可以在模型中添加修改后的名稱。
protected $table = "classes_teacher"
uj5u.com熱心網友回復:
你搞砸了你的人際關系。你宣告Classes-> Teacheras BelongsToMany,所以 Laravel 試圖找到一個資料透視表來建立關系。但是您的遷移顯示classes有一個teacher_id列,表明它應該是簡單的BelongsTo.
class Classes extends Model
{
public function teacher()
{
return $this->belongsTo(Teacher::class);
}
}
您還進行了錯誤的遷移,未設定外鍵,并且還使用了錯誤的列型別。它應該如下所示:
public function up()
{
Schema::create('classes', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->foreignId('course_catagory_id')->constrained();
$table->foreignId('teacher_id')->constrained();
$table->timestamps();
});
}
您的其他遷移需要以類似的方式重做。
而且,您拼錯了“類別”。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/406398.html
標籤:
上一篇:SQLSTATE[42S02]:未找到基表或視圖:1146表“mkstudent.classes_student”不存在(SQL:插入“classes_student”(“classes_id”,
