Guys. help me working with array inputs on my project. this is the form :
{{Form::open(array('route' => array('route.name', $user->id)))}}
{!!csrf_field()!!}
{{Form::text('year_id', '')}}
{{Form::text('grade_id', '')}}
{{Form::text('clas_id', '')}}
{{Form::text('clas_id', 'student_id')}}
@foreach ($lessons as $key => $value)
{{Form::text('scores[]', '')}}
{{Form::text('lesson_id[]', '{{\App\Models\ClassLesson::find($value->id)->lesson->id}}')}}
@endforeach
{{Form::close()}}
this is my Model:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Assestment extends Model
{
protected $table = 'assestments';
protected $fillable = [
'year_id', 'grade_id', 'clas_id', 'student_id', 'lesson_id', 'score',
];
public function students()
{
return $this->belongsToMany('App\Models\Student');
}
public function lessons()
{
return $this->belongsToMany('App\Models\Lesson');
}
}
and this is my Controller:
public function store(Request $request)
{
foreach ($request->scores as $score)
{
$scr = new Assestment;
$scr->year_id = $request->year_id;
$scr->grade_id = $request->grade_id;
$scr->clas_id = $request->clas_id;
$scr->student_id = $request->student_id;
foreach($request->lesson_id as $lesson)
{
$scr->lesson_id = $lesson;
}
$scr->score = $score;
$scr->save();
}
}
i would like to make the value of field "scores[]" and "lesson_id[]" as array so that they store multiple data to database. but when i submit the form, the field "lesson_id[]" always store the last value to the database. for example, the field "lesson_id[]" contains multiple values like "1, 2, 3 , 4", but the value stored into database is always "4 (the last value of the field)".
i wanna store the multiple values value of lesson_id[] like:
{{Form::text('scores[]','', array('class'=>'form-control'))}} with vales (80, 60, 60, 70, 80, etc)
{{Form::hidden('lesson_id[]', '')}} with values (10, 2, 6, 9, 8, etc)
where the value of the lesson_id[] is generated based on the id of the lessons table. imagine, in a class room, there are many lessons. the lessons are stored inside database named lessons. i want to retrieve the id of that lessons as the value of the field lesson_id[] (of course many lessons) like the example above. whenever i submit the form, the lesson_id[] always store the last value (let say 8 on the example above). yet, the field score[] has no problem and always store data based on input provided.
please help. im running out of idea..