Home > database >  SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mkstudent.classes_student' does
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mkstudent.classes_student' does

Time:01-06

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="{{route('students.store')}}" method="POST" enctype="multipart/form-data" >
            @csrf
            <div >
                <label for="title">First Name</label>
                <input type="text" name="first_name"  value="">
            </div>
            <div >
                <label for="title">Last Name</label>
                <input type="text" name="last_name"  value="">
            </div>
            <div >
                <label for="title">Phone Number</label>
                <input type="text" name="phone_number"  value="">
            </div>
            <div >
                <label for="Class">Class</label>
                @if($classes->count() > 0)
                <select name="class[]" id="class" multiple="true" >
                    @foreach($classes as $class)
                    <option value="{{$class->id}}">
                        {{$class->name}}
                    </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);
    }

CodePudding user response:

In relations (in both models) as second parameter you must provide table name class_student because you don't follow convention because you model name is Classes

CodePudding user response:

You can pass table name as second parameter in 'classe' relation like this:

public function classe()
    {
      return $this->belongsToMany(Classes::class, 'class_student');
    }
  •  Tags:  
  • Related