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

achieve100's avatar

@Snapey Thanks. Unfortunately, still nothing happened after modifying to your suggested code. My jquery is still pointing to this form table. I got a message that I created "Those records are successfully updated!" But, don't really create anything in the db table. Please advise what else I can do. Thanks.

@jlrdw Thanks for your response. But, honestly, yours were not related to my case.

Snapey's avatar

This loops us back to my first posting on this thread which was to be able to check what is being posted to the server from your form.

There are a couple of ways your code can just skip doing anything yet still return 'Those records have been successfully updated!' ...this means nothing.

if (is_array($inputs) || is_object($inputs))

well, you are hoping its an array - but that depends on how you have named the form fields

It will NEVER be an object

achieve100's avatar

I could see all input data in the inspect web coding tool in Google. Just don't understand why they are not inserting to the dab table? Please advise.

Snapey's avatar

Its about HOW the data is arranged

Are you getting an array of rows each with a complete set of values

achieve100's avatar

@Snapey I tried to insert 3 rows of data.

But,

  1. I only got the first row of data shown. The other 2 have empty data except showing Date and Badge fields.

  2. no number assigning into [] field. Does it suppose to be like [0], [1] and [2] or it doesn't matter? Please advise.

Snapey's avatar

I can't tell. You are not sharing...

Snapey's avatar

If you look at which entries have values then what you saw written to the database looks correct, but what happened to rows

achieve100's avatar

You mean the "rows" I used it in the controller?

class UpdateLogController extends Controller
{
       public function update_log(Request $Request){

            
                $inputs = Input::get('rows');
                $Timetracks = array();

This "rows" is to used for the jquery code which I shared b4. One of the name="rows['+i+'][Date]", etc in app.blade.php file. I am not sure if I can get rid of jquery in order not too complicated and I can use the controller to totally control to insert multiple rows of data to the db table.

In home.blade.php, I used name="Date[]". Thanks.

Snapey's avatar

Its impossible to advise. As mentioned, the way the data is presented in the $request is critical.

add this to the top of the update_log() function

return json_encode(request()->all());

and then submit your form and paste the response here

Snapey's avatar

This is the way your data is presented to the controller

{
  "_token": "oYTgHxS7JZyuky81sCP8XMHSNqiSjRdZFS4I8per",
  "btn-save": null,
  "Date": [
    "2017-05-31",
    "2017-05-31",
    "2017-05-31",
    "2017-05-31"
  ],
  "Badge": [
   1,
   2,
   3,
   4,
  ],

etc

etc


  "OutTimeHour": [
    "2"
  ],
  "OutTimeMinute": [
    "3"
  ],
  "InTimeHour": [
    "3"
  ],
  "InTimeMinute": [
    "4"
  ]
}

You will see that you have place for 4 values in the form, but for the majority of the fields they are null for the first three entries - as you have seen in the database.

Then you have the other problem for the in and out times - only one value is presented.

Boy, I wish you would just comment out most of the form until this is working

The way the data is now, you need to initialise an iterator and then use this to key into each parameter. Or, you can rearrange the form so that you have 4 records each one of which has one named value.

You need to work on the form to start with (sounds familiar) .

Can you come up with a smaller form to work with and then add more fields later?

achieve100's avatar

Let me try to explain the situation and environment here. The goal is to insert whatever a user query from the database table. Then, input some values like OutTime and InTime from the drop down menu, then, insert the whole things to the new table. This is the goal.

That's why I don't have a named value to each row. They are all generated by foreach in home.blade.php. And that's why I named each field is Date[], Badge[], CEN[], etc. I don't know what else I can modify the form because Date and Badge are no issue.

Also, you are correct that there is time insert issue too. Sure. Let me start from the simple form first. But, then, I am not sure how to assign name to foreach loop since they are not really in the home.blade.php file. That I don't know how to figure out? Thanks.

Snapey's avatar

I'll try and describe how form arrays work.

If you name an element name='hello' then this can only contain one value. If you repeat the element multiple times on the page, only the last instance will be sent to the server.

If you name an element with square braces immediately after name='hello[]' then when you repeat this element on the form, the server will receive an input called hello containing an array of values, one for each hello field.

If you name an element with a name and then square braces and then another name name='rows[]hello' then when you repeat this element on the form you get an input called rows which contains an array of rows, each of which has a 'hello' field.

In this latter example, you can iterate over the rows;

foreach($request->rows as $row)
{
    $hello = $row['hello'];
    $there = $row['there'];
    $world = $row['world'];
}

You can also use laravel array validation;

$rules= [
    'rows.*.hello' =>'required|max:20', 
    'rows.*.there' =>'required|max:100',    
    'rows.*.world' =>'required|max:50', 
];

So, if you are dealing with repeated form elements the name[]name approach is definitely the way to go.

If you use empty braces like this then what is returned is a simple array. You can also key the array too. So if for instance, editing a list of activities;

@foreach($activities as $activity)

    <input name='activities[{{ $activity->id }}]title' /> 
    <input name='activities[{{ $activity->id }}]desc' /> 
    <input name='activities[{{ $activity->id }}]duration' /> 

Does this help you to sort out the naming of the form fields?

jlrdw's avatar

In this post https://laracasts.com/discuss/channels/laravel/how-to-save-a-multiple-rows-of-records-from-one-database-table-to-another-table-at-the-same-database-in-laravel-54

I demoed array, inserting data from one table to a backup table, and going back and updating the checkboxes to checked (printed) in the first table.

That demo was meant for you to at least get some basic ideas from. You are going at this stuff in laravel without at least a few months of the basic knowledge which is why you are having so much trouble.

If you are serious about being a developer you need to take a breath slow down and actually learn this stuff.

achieve100's avatar

Thanks Snapey. I will take your advice and continue working on it. By the way, can u do me a favor? Would u please to remove the post that u asked me to paste the response? Cos I didn't pay attention that the response got values. Thanks.

jlrdw's avatar

@Snapey maybe show OP how to iterate through a set of records, get some needed data, and insert the data into another table.

Been what 50 conversations on it and still no solution.

Just learn iteration. Also known as looping over, etc.

Snapey's avatar

The first part of this thread was about having two form fields which together make up a time value.

The second part of the thread is about constructing arrays in forms.

We have not got as far as iterating over the data yet.

@jlrdw

Previous

Please or to participate in this conversation.