SQLSTATE[42S22]: Column not found: 1054 Unknown column 'students_id' in 'where clause' (SQL: select * from students_subjects
where students_id
= 71) Student Controller
public function store(Request $request)
{
$input =$request->all();
$user =Auth::user();
if ($file = $request->file('photo_id')){
$name = time().$file->getClientOriginalName();
$file->move('images',$name);
$photo = Photos::create(['file'=>$name]);
$input['photo_id']=$photo->id;
}
$student = $user->student()->create($input);
if ($request->subject_id){
$student->subject()->sync($request->subject_id);
}
dd($student);
return redirect('admin/students');
//
}
Student_Subject_table.php
public function up()
{
Schema::create('students_subjects', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('student_id');
$table->unsignedBigInteger('subject_id');
$table->foreign('student_id')->references('id')->on('students');
$table->foreign('subject_id')->references('id')->on('subjects');
$table->timestamps();
});
}
Students Model
class Students extends Model
{
protected $table = 'students';
protected $fillable=['user_id','FullName','age','address','father_ID','photo_id','class_id'];
public function subject(){
return $this->belongsToMany(Subjects::class);
}
}
Subjects Modal
class Subjects extends Model
{
protected $table = 'subjects';
protected $fillable = ['name','user_id'];
//
public function student()
{
return $this->belongsToMany(Students::class);
}
//
}
create.blade.php
<div class="form-group form-check form-check-inline">
<span class="caption-subject font-dark bold uppercase">Subject :</span>
@foreach($subjects as $subject)
<input type="checkbox" value="" name="subject_id[]" class="form-check-input">
<label class="form-check-label "></label>
@endforeach
</div>
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire