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

asadsajjad's avatar

Checkbox value in controller in laravel

I am trying to get all the payments that is been selected through the checkboxes and then update the value in the database by '1' which means its paid. But when I try to get the values of checkbox array I only get 1 data in the array and not all selected. Any help?

View

@foreach($sellerID as $p)
     <form action="/paid/{{$p->id}}" method="post" enctype="multipart/form-data">   
         @csrf
 <td><input type="checkbox" name="checked[]" value="{{ $p->id }}"></td>
 <td> {{$p->order_number}} </td>
 <td>
     <input id="commission" type="text" class="form-control" name="commission" 
     value="{{ $p->commission_value }}" readonly  autocomplete="notes" style="width: 15%;" autofocus>
 </td>
                                    
 <td> {{$p->product_name}} </td>
 <td> 
     @if($p->sellet_commission_paid == '0')
        <button type="submit" class="btn btn-default" style="background-color:red">
            <b><em>UNPAID</b></em>
        </button>
     @else
        <a href="#" class="btn btn-default" style="background-color:green"><b><em>PAID</b></em></a>
     @endif                                    
 <td>   
  </form>
   @endforeach

Controller

 $checked = $request->input('checked');
 //   $checkboxes = $request->checked;

 dd($checked);
 die();
//    dd(che
0 likes
24 replies
Snapey's avatar

I would probably give each checkbox a unique name and have value =1

<input type="checkbox" name="checked[]{{ $p->id }}" value="1">
asadsajjad's avatar

@snapey I am only getting one item from the checkbox in the array not all what are selected. I tried what you suggested but its still the same

Snapey's avatar

ah, I see it

Its because your form only contains one checkbox. You are creating multiple forms because the form tag is inside your foreach loop

asadsajjad's avatar

I got it but I tried to move the form tag outside the foreach loop and then it gives error on id Property [id] does not exist on this collection instance

asadsajjad's avatar

yes @jlrdw I am trying to update the selected payments to PAID. I have updated my code with the help of @snapey but now its not updating all of it. Below is my updated code

View

                      @if(count($sellerID)>0)
                            <form action="/paid" method="post" enctype="multipart/form-data">

                                @foreach($sellerID as $p)
                                    <tr>
                                            @csrf
                                            <td><input type="checkbox" name="checked[]" value="{{ $p->id }}"></td>
                                            <td> {{ $p->order_number }}
                                            <td>
                                                <input id="commission" type="text" class="form-control" name="commission"
                                                                value="{{ $p->commission_value }}" readonly  autocomplete="notes" style="width: 15%;" autofocus>
                                            </td>
                                        
                                            <td> {{$p->product_name}} </td>
                                            <td> 
                                                @if($p->sellet_commission_paid == '0')
                                                    <button type="submit" class="btn btn-default" style="background-color:red">
                                                        <b><em>UNPAID</b></em>
                                                    </button>
                                                @else
                                                    <a href="#" class="btn btn-default" style="background-color:green"><b><em>PAID</b></em></a>
                                                @endif                                    
                                            <td>   
                                    
                                    </tr>
                                @endforeach
                            </form>
                            </tbody>  
                        @endif

Controller

              // $findPaymentId = Payment::find($id);
            
            $commissionValue = Input::get('commission');

            $checked = $request->input('checked');
     
        foreach($checked as $c) {

            $findPaymentId = Payment::where('id', '=', $c)->first();

            // echo $findPaymentId->commission_value;
            // die();

            if($findPaymentId->commission_value == $commissionValue) {
                $updateStatus = Payment::where('id', $c)->update([
                            
                    // 'status' => '1',
                    'sellet_commission_paid' => '1',
                    
                
                ]);

                Session::flash("message", "You confirmed that you have received the commission.");
                return redirect('/seller-payments');
       }
Snapey's avatar

it gives error on id Property [id] does not exist on this collection instance

when.....

when you show the page?

when you submit the form to the controller?

What line does it error on?

jlrdw's avatar

@asadsajjad you are wanting to manually click checked? Something like editing each seller manually?

Have you considered datables? Or JS / ajax which I use.

Again sorry if I am not understanding.

I deleted other reply because I am not 100% sure what you are doing, yet I'd like to try and help.

Please, if I am way off base understanding what you are trying to do, please ignore my answer.

Otherwise just let us know if that is or is not correct, you want to manually edit individual records.

Snapey's avatar

@jlrdw simple checkboxes in a form select 2 or 3 and press submit. Nothing more complicated than that

asadsajjad's avatar
asadsajjad
OP
Best Answer
Level 1

this is the updated code as but now this is not taking all values and passing to the database to update. The code seems to be fine. Am I doing something wrong in the view file?

@snapey @jlrdw

Controller

 $commissionValue = Input::get('commission');

            $checked = $request->input('checked');
     
        foreach($checked as $c) {

            $findPaymentId = Payment::where('id', '=', $c)->get();

            // echo $findPaymentId->commission_value;
            // die();

            foreach($findPaymentId as $f) {
                if($f->commission_value == $commissionValue) {
                    $updateStatus = Payment::where('id', $f->id)->update([
                            
                        // 'status' => '1',
                        'sellet_commission_paid' => '1',
                        
                    
                    ]);


    
                } elseif($findPaymentId->agent_commission == $commissionValue) {
                    $updateStatus = Payment::where('id', $id)->update([
                                
                        // 'status' => '1',
                        'agent_commission_paid' => '1',
                    
                    ]);


                    
                }
             
            }

                    Session::flash("message", "You confirmed that you have received the commission.");
                    return redirect()->back();
        }

View

          @if(count($sellerID)>0)
                            <form action="/paid" method="post" enctype="multipart/form-data">

                                @foreach($sellerID as $p)
                                    <tr>
                                            @csrf
                                            <td><input type="checkbox" name="checked[]" value="{{ $p->id }}"></td>
                                            <td> {{ $p->order_number }} </td>
                                            <td>
                                                <input id="commission" type="text" class="form-control" name="commission"
                                                                value="{{ $p->commission_value }}" readonly  autocomplete="notes" style="width: 15%;" autofocus>
                                            </td>
                                        
                                            <td> {{$p->product_name}} </td>
                                            <td> 
                                                @if($p->sellet_commission_paid == '0')
                                                    <button type="submit" class="btn btn-default" style="background-color:red">
                                                        <b><em>UNPAID</b></em>
                                                    </button>
                                                @else
                                                    <a href="#" class="btn btn-default" style="background-color:green"><b><em>PAID</b></em></a>
                                                @endif                                    
                                            <td>   
                                    
                                    </tr>
                                @endforeach
                            </form>

                        </tbody>  
                        @endif
asadsajjad's avatar

how can I share an image here may that'll be for you to understand my problem?

asadsajjad's avatar

@jlrdw I am just trying to update the rows based on the checkboxes checked by the user.

Snapey's avatar

why not just answer the last questions?

asadsajjad's avatar

@snapey sorry about it. Those error already got fixed with your suggestion on taking the form out of the foreach loop. But now on controller I have checked everything it is passing every value but when I click on PAY it just update one row

asadsajjad's avatar

it got fixed @snapey and @jlrdw. I was returning it to the page before the loop processes all of the inputs. Thank you for your messages and help. I really appreciate it.

Now the only problem that is left is that when I update seller payment it should redirect back to seller payment page but because of this foreach loop I cant seem that to be possible

Snapey's avatar

change this


            $findPaymentId = Payment::where('id', '=', $c)->get();

to find, then you won't need the second foreach loop because it will only return the model and not a collection

           $findPaymentId = Payment::find($c);
asadsajjad's avatar

@snapey I did changed that to get and it got fixed. I think its their server issue which is delaying the messages from both ends

Snapey's avatar

->get() is NOT the right thing to use, unless you pass all the checkbox values into a whereIn query

meeshal's avatar

Just use a variable inside the for loop in place of return.

return redirect('/agent-payments'); will become $return = '/agent-payments'; and at the end outside of the loop, just say return redirect($return);

asadsajjad's avatar

@meeshal thank you for your answer but what if none is selected and redirect on the same page on null values?

meeshal's avatar

define this $return variable on top of your function with a default value.

like this $return = '/default-redirect';

So, if there is no checkbox selected (which will not happen if your workflow is correct), it will redirect to the default route/address.

Thanks

asadsajjad's avatar

return redirect()->back(); this is more appropriate way of doing it. This worked for me. Thanks though

Thank you everyone @snapey @jlrdw @meeshal

1 like
Snapey's avatar

glad you were able to contribute best answer. It makes it so worthwhile

1 like
pilat's avatar

One notice on on your blade file: you only need that @csrf once — right after the form is opened:

@if(count($sellerID)>0)
    <form action="/paid" method="post" enctype="multipart/form-data">
    @csrf 
        
        @foreach($sellerID as $p)
            <tr>
                <td><input type="checkbox" ...
1 like

Please or to participate in this conversation.