Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

danisemet's avatar

Array text-field always store the last value into database.

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....

0 likes
18 replies
ahuggins's avatar

The way you wrote the code, I would expect it to work that way.

each time it loops the scores, it loops the lesson_ids, so every score->lesson_id is going to be set to the last $lesson.

You might want to change your form so that the arrays are named.

What does your form code look like?

1 like
danisemet's avatar

Just like the above. The field "score[]" works fine and store the multiple value to database based on input. but the field "lesson_id[]" always store the last value of the array. plis help..

danisemet's avatar

Come on guys.,.. give some idea. i really need to get it work.. plis..

ahuggins's avatar

Do this:

public function store(Request $request)
    {
    $count = 0;
    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;
    $scr->lesson_id[$count];
        $scr->score = $score;
        $scr->save();
    $count++;
    }
  
}
danisemet's avatar

noo... i don't want it to generate random number from "$count++". but i want it to save the multiple value accepted from "@foreach ($lessons as $key => $value)".. i'd like to make a scoring system for students in a class. the "lesson_id[]" is the list of lessons in a given class room. so, wan that the "lesson_id[]" field will hold all the lesson of a given class in an array that is received via "@foreach ($lessons as $key => $value)".. i don't mean to make a random number with it .

ahuggins's avatar

show a sample of what you want stored in $src->lesson_id it's hard to figure that out from your description.

ahuggins's avatar

Or maybe what you want is as simple as:

   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();
    }
danisemet's avatar

i got this message when applying that change


ErrorException in AssestmentController.php line 97:
Indirect modification of overloaded property App\Models\Assestment::$lesson_id has no effect
danisemet's avatar

i wanna store the multiple values value of lesson_id[] like:


@foreach ($lessons as $key => $value)
{{Form::text('scores[]','', array('class'=>'form-control'))}}
{{Form::hidden('lesson_id[]', \App\Models\ClassLesson::find($value->id)->lesson->id)}}
@endforeach
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 id of the last "lesson" that was generated via @foreach loops. yet, the field score[] has no problem and always store data based on input provided.

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();
    }

}
tykus's avatar

If you are looking to store the contents of lesson_id in a single database field, then you can use implode()

implode($request->lesson_id, ',');

which will result in a comma-separated list.

danisemet's avatar

the value of lesson_id is not a comma-sparated. they are some normal text fields that was populated based on the number of lessons in each class room. for examples, a classroom has 10 lessons to be learnt. and these 10 lessons are stored inside a table named lessons. of course these lessons have column id. what i do is just retrieve the id of each lessons as the value of the text-field via @foreach loop to create multiple text-field. if the class room has 10 lessons, of course there will be 10 text-fields containing the value equal to the id of each lesson. all i want to do is just give scores based on the lessons. if there are 10 lessons, then there will be 10 text-fields containing the id of the lessons and 10 text-fields for the scores. but everytime i submit the form, the score stored into dabase is based on input, but the lesson_id[] always store the last id. for example:


{{Form::text('lesson_id[]', '1')}}
{{Form::text('lesson_id[]', '2')}}
{{Form::text('lesson_id[]', '5')}}
{{Form::text('lesson_id[]', '7')}}
{{Form::text('lesson_id[]', '12')}}
the value stored into database is always "12". do you guys get what i mean?
danisemet's avatar

im sure that the problem is with my controller, but o dont know what to change..

tykus's avatar
tykus
Best Answer
Level 104

So, do you want one Assestment record for each lesson/score combination - you have a single lesson_id column in the table, so only one lesson_id will be stored for each Assestment record, right?

public function store(Request $request)
{

    foreach ($request->scores as $index => $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;
        $scr->lesson_id = $lesson_id[$index]; // here we lookup the item in lesson_id[] with the same index as the current score item
        $scr->score = $score;
        $scr->save();
    }

}

1 like
danisemet's avatar

@tykus_ikus wow. you are my hero. you solve my problem that easy. what a genius.. thank you so much.

danisemet's avatar

@ahuggins thank you very much for your support. thanks for giving your time to take attention to a noob like me. for all you guys who have post comments here, i really thank you.

ahuggins's avatar

The accepted answer does the same thing as the one I posted:

public function store(Request $request)
    {
    $count = 0;
    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;
    $scr->lesson_id[$count];
        $scr->score = $score;
        $scr->save();
    $count++;
    }
  
}

It just accomplishes it in a different way. I'm guessing you didn't even try it?

1 like
danisemet's avatar

@ahuggins it causes an increment, Master. the lesson_id[] stored into database is not based on input. but an automatic increment generated by $count++. i have tried it. Thank you so much for giving me some additional knowledge for future use.. Once again. thank you..

Please or to participate in this conversation.