Return key of an Array from given value

Published 4 months ago by nizam0786

Hi guys,

I am a bit stuck on a function I am writing up.

I am posting a form which contains a hidden id input, a input for serial number and a checkbox which is clicked if the object is printed or not.

Form values

 

input name="serials[]" type="text" value="633597 0107 8137" size="21"

input name="passesPrinted[]" type="checkbox" value="{{$pass->id}}"

input class="hidden" name="passes[]" type="text" value="{{$pass->id}}" readonly

So after the form is posted I need to update only the object which has the checkbox selected.

The way I am trying to do that is searching for the value of checkbox in the passesPrinted array and return the key so I can just loop through and save the values.

However, I am not sure how to get the key etc...

Thanks in advance.

Best Answer (As Selected By nizam0786)
lostdreamer_nl

Aah, now I see.... The easiest fix would then be to add the id of the pass as the index in all it's form fields:

 <input name="serials[{{ $pass->id}}]" type="text" value="633597 0107 8137" size="21">

<input name="passesPrinted[{{ $pass->id}}]" type="checkbox" value="{{$pass->id}}">

<input class="hidden" name="passes[{{ $pass->id}}]" type="text" value="{{$pass->id}}" readonly>

Then in your controller, $key will be the same as $id, and $key will always align between $request->passessPrinted and $request->serials

$passes = Pass:whereIn('id', $request->passesPrinted)->get()->keyBy('id');
foreach($request->passesPrinted as $key => $id) {
    // Pass $id  has been checked
    // you can also get it's 'serial' by:  $request->serials[$key]
    // $passes[$id] holds the Pass Object for this $id
    $passes[$id]->update(['field' => 'new value']);
}
lostdreamer_nl

I don't know if I understand what you are trying to do exactly, but I think it's along these lines:

$passes = Pass:whereIn('id', $request->passesPrinted)->get()->keyBy('id');
foreach($request->passesPrinted as $key => $id) {
    // Pass $id  has been checked
    // you can also get it's 'serial' by:  $request->serials[$key]
    // $passes[$id] holds the Pass Object for this $id
    $passes[$id]->update(['field' => 'new value']);
}

It get's all passes from the DB having an id that was within the checked checkboxes. Then it loops over all passesPrinted id's and for each you can update it.

nizam0786

@lostdreamer_nl thank you for you reply yes I know its a bit confusing but I will have a go and let you know.

nizam0786

@lostdreamer_nl

I come across an issue basically the checkbox key is different to the key of the serials array so I am saving the wrong serial to the wrong pass if that makes sense.

it will only work if i select every single checkbox so the keys match. Let says if I only select the second checkbox in the list it wont start [1] it will start form [0] again so I need it to match the key in the serial etc..

hope that makes sense.

nizam0786

updating is not the issue its pointing the right row in the array to the right pass only if the checkbox has been selected.

nizam0786

the main aim is basically having a table where I can input values and then only posting the rows of data on form if checkbox is selected.

lostdreamer_nl

Aah, now I see.... The easiest fix would then be to add the id of the pass as the index in all it's form fields:

 <input name="serials[{{ $pass->id}}]" type="text" value="633597 0107 8137" size="21">

<input name="passesPrinted[{{ $pass->id}}]" type="checkbox" value="{{$pass->id}}">

<input class="hidden" name="passes[{{ $pass->id}}]" type="text" value="{{$pass->id}}" readonly>

Then in your controller, $key will be the same as $id, and $key will always align between $request->passessPrinted and $request->serials

$passes = Pass:whereIn('id', $request->passesPrinted)->get()->keyBy('id');
foreach($request->passesPrinted as $key => $id) {
    // Pass $id  has been checked
    // you can also get it's 'serial' by:  $request->serials[$key]
    // $passes[$id] holds the Pass Object for this $id
    $passes[$id]->update(['field' => 'new value']);
}
nizam0786

@lostdreamer_nl thank you! it worked :) much appreciated.

Please sign in or create an account to participate in this conversation.