Guys. help me working with array inputs on my project.
this is the form :
@foreach ($lessons as $key => $value)
{{Form::open(array('route' => array('route.name', $user->id)))}}
{!!csrf_field()!!}
{{Form::text('scores[]', '')}}
{{Form::text('lesson_id[]', '{{\App\Models\ClassLesson::find($value->id)->lesson->id}}')}}
{{Form::text('year_id', '')}}
{{Form::text('grade_id', '')}}
{{Form::text('clas_id', '')}}
{{Form::text('clas_id', 'student_id')}}
{{Form::close()}}
@endforeach
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)".
what should i do? plis help....