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

polarcubs's avatar

Updating Multiple Models in a single Form, how to improve?

Hi,

I was writing code to update multiple models (4 students) in a form. I did something like this:

<textarea name="description-{{$student->id}}"
                              id="description-{{$student->id}}"
                              placeholder="You may use markdown for this field">
</textarea>

Then in my controller, I do a split to find out the description field is for which student as a student of id 1 will be description-1.

It works but somehow I felt that it could be better handled. If you are wondering, the reason why my page edit 4 students at once is a client requirement. Each class has 4 students and they do not want to go separate pages to edit their information.

Thank you.

0 likes
9 replies
pmall's avatar
pmall
Best Answer
Level 56

Like any other form you can name the fields in such a way that arrays are sent :

<textarea name="students[{{ $student->id }}][description]"></textarea>

Then in your controller :

$students = $request->get('students');

foreach($students as $id => $student)
{
  // Logic here with $id, $student['description'], etc...
}
michaeldyrynda's avatar

Set the field name to description[{{ $student->id }}] and the same for each of your other fields.

You can then access description as an array, keyed on the student's id.

Edit: Damn @pmall... need to stop posting from my phone lol

polarcubs's avatar

Wow @pmall, thanks for the tip, this is something I didn't know about previously.

Then to the question of validation.

Because the validation are repeated for each model, is there a elegant way to duplicate the validations in the models/controller and display it on frontend?

Update: I'm using Laravel 4.2 with laravel validating package.

bobbybouwmann's avatar

We use dependency injection to make sure that the right request object is used. The create function will automatically use the rules in the StudentRequest class

StudentController.php

public function store(StudentRequest $request) 
{
    Student::create($request->all());
}

Now we need to create the students request class (You can generate it with php artisan make:request StudentRequest)

StudentRequest.php

<?php namespace App\Http\Requests;

use App\Http\Requests\Request;

class StudentRequest extends Request {

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true; // The user is always permitted to do this, so we perform no action here
    }

    /**
     * Get the validation rules that apply to the student request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required|min:3',
            'description' => 'required'
        ];
    }

}
pmall's avatar

@blackbird he is on L4

@polarcubs no other solution than constructing an array of rules somehow, according to the number of students in the request input. Then use dot notation :

$students = Request::get('students');

$rules = [];

for($i = 0; $i < count($students); $i++)
{
  $rules['students.' . $i . '.description'] = '...';
}
polarcubs's avatar

Hi @pmall, I noticed you use $request->get('students') instead of Input::get('students').

Is there any differences?

pmall's avatar

$request->get('students') is more L5ich :D

Please or to participate in this conversation.