Postingan lainnya
Gimana cara insert data many-to-many?
gan mau nanya nih cara nya mau insert data ke table skali input lngsung banyak gtu gmn ya? contoh nya kyk assign beberapa murid dlm 1 kelas gtu
ni controller store nya
public function saveAssign(Request $request,$classId)
{
$input = $request->all();
$messages = array(
'studentClassId.required' => 'Student class ID required.',
'scClassId.required' => 'Class required.',
'scStudentId.required' => 'Student required.',
);
$rules = array(
'studentClassId' => 'required|min:16|max:50',
'scClassId' => 'required|min:16|max:50',
'scStudentId' => 'required|min:16|max:50',
);
$validator = Validator::make($input,$rules,$messages);
if($validator->fails())
{
#Return to the same page with error message
return Redirect::back()->withErrors($validator)->withInput();
#If succeed
}
$studentClass = new StudentClass();
$studentClass->studentClassId = $input['studentClassId'];
$studentClass->scClassId = $input['scClassId'];
$studentClass->scStudentId = $input['scStudentId'];
if(!$studentClass->save())
App::abort(500);
return Redirect::action('Classroom\ClassroomController@view_participant',$classId)->with('successMessage','Students have successfully assigned.');
}
ini Model nya si classroom
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Classroom extends Model
{
protected $table = 'classes';
public $incrementing = false;
public $timestamps = false;
protected $primaryKey = 'classId';
protected $fillable = array('classTeacherId','classTrackId','classNumber','className','classStatus');
public function Student()
{
return $this->belongsToMany('App\Student','student_classes','scClassId','scStudentId');
}
public function Teacher()
{
return $this->hasOne('App\Teacher','foreign_key','teacherId');
}
public function Track()
{
return $this->hasOne('App\Track','foreign_key','trackId');
}
}
ini model nya si student
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Student extends Model
{
protected $table = 'students';
public $incrementing = false;
public $timestamps = true;
protected $primaryKey = 'studentId';
protected $fillable = array('studentId','studentPic','studentNIS','studentName','studentGender','studentReligion','studentPoB','studentDoB','studentNationality','studentAddress','studentPhone1','studentStatus');
//protected $dates = 'studentDoB';
public function Class()
{
return $this->belongsToMany('App\Classroom','student_classes','scStudentId','scClassId');
}
}
mohon bantuannya ya gan hehe
0