jeudi 6 janvier 2022

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mkstudent.classes_student' doesn't exist (SQL: insert into `classes_student` (`classes_id`,

i dont know why it is going to store in mkstudent.classes_student table , i mean i created many to many table class_student and when i tray to store a a student that have a class this error is happening any one who can help me please? the code is here student controller

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="" 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="">
                        
                    </option>
                    @endforeach
                </select>
                @endif
            </div>

and the migration for class_student is

 public function up()
    {
        Schema::create('class_student', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('classes_id');
            $table->integer('student_id');
            $table->timestamps();
        });
    }

and in the student model


class Student extends Model
{
     protected $fillable = [
        'first_name','last_name','phone_number'
    ];

    public function classe()
    {
      return $this->belongsToMany(Classes::class);
    }
}

in the classes model

 public function students()
    {
        return $this->belongsToMany(Student::class);
    }


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire