here is my Classes model
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);
}
}
and migrations
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();
});
}
and create classes page
<div >
@if(session()->has('success'))
<div >
{{session()->get('success')}}
</div>
@endif
<form method="POST" action="{{ route('courses.store')}}">
@csrf
<table >
<tr>
<th>Name</th>
<td>
<input type="text" name="name" >
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="submit" >
</td>
</tr>
</table>
</form>
</div>
</div>
and classes index page
<div >
@if($data->count()>0)
<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 >No class Yet!</h3>
@endif
</div>
and when i create a new class it saved in the database but dose not restricted to the index view and when i tray to see the index view only the class name is displayed the other fields are not dispalyed and the error face the error
CodePudding user response:
What happens if you update your Classes model with a $table protected property such that:
protected $table = 'classes_teacher'
Or, whatever the table name is supposed to be ?
CodePudding user response:
'mkstudent.classes_teacher' doesn't exist (SQL: select
teachers.*,classes_teacher.`cla ...
The problem is that classes_teacher is not plural. I should be named classes_teachers. But you can add in your model the modified name.
protected $table = "classes_teacher"
CodePudding user response:
You have messed up your relationships. You declare Classes->Teacher as BelongsToMany, so Laravel is trying to find a pivot table to build the relationship. But your migration shows that classes has a teacher_id column, suggesting it should be simply BelongsTo.
class Classes extends Model
{
public function teacher()
{
return $this->belongsTo(Teacher::class);
}
}
You've also done migrations wrong, with foreign keys not set up as such, and also using the wrong column type. It should look like this:
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();
});
}
Your other migrations would need to be redone in a similar fashion.
And, you've misspelled "category."
