Rajendra
Rajendra

Reputation: 393

How to get multiple rows data of the form in controller in laravel 5.1

I have to show/edit a form in my page. I'm fetching all rows from table and showing in table format which is editable. Once the table is edited. I want all those rows to update in database using controller, however I dont know how to get all form rows through request object in laravel 5.1.

$records = $request->all();

As request object giving only last row of the form. Any suggestion?

@section('content')
{!! Form::open(array('url' => 'madcat/update', 'method' => 'put', 'class'=>'form-horizontal')) !!}
<input type="hidden" name="_token" value="{!! csrf_token() !!}">
    <div class="form-group">                
        <table class=" table table-condensed">
        <thead> 
	        <tr> 
		        <th>#</th>
		        <th>Field</th> 
		        <th>Max</th>
	            <th>Min</th>
	        </tr> 
        </thead>
        @foreach($records as $key => $value)
          <tr>
          	<td>{!!Form::text('id',$value->id,['readonly'])!!}</td>
            <td>{!!Form::checkbox('Field_check'.$value->id)!!}{!! Form::text('Field',$value->Field,['readonly'])!!}</td>
            <td>{!!Form::checkbox('Max_check'.$value->id)!!}{!! Form::text('Max',$value->Max,['readonly'],array('size'=>'4'))!!}</td>
            <td>{!!Form::checkbox('Min_check'.$value->id)!!}{!!Form::text('Min', $value->Min,['readonly'],array('size'=>'4'))!!}</td>
          </tr>
        @endforeach
        </table>
        <div class="col-md-6 col-md-offset-11">
        {!!Form::submit('Save', array('class'=>'btn btn-primary'))!!}
        
		</div>
    </div>
    {!!Form::close()!!}
@endsection

This is the code of HTML. Here I will check the checkbox in front of all the rows element so that I want to update only those rows in another table.

Upvotes: 1

Views: 1724

Answers (2)

Rajendra
Rajendra

Reputation: 393

Here I got the answer . Actually mistake was the naming of all the form elements

@section('content')
{!! Form::open(array('url' => 'madcat/update', 'method' => 'put', 'class'=>'form-horizontal')) !!}
<input type="hidden" name="_token" value="{!! csrf_token() !!}">
    <div class="form-group">                
        <table class=" table table-condensed">
        <thead> 
	        <tr> 
		        <th>#</th>
		        <th>Field</th> 
		        <th>Max</th>
	            <th>Min</th>
	        </tr> 
        </thead>
        @foreach($records as $key => $value)
          <tr>
          	<td>{!!Form::text('id'.$value->id,$value->id,['readonly'])!!}</td>
            <td>{!!Form::checkbox('Field_check'.$value->id)!!}{!! Form::text('Field'.$value->id,$value->Field,['readonly'])!!}</td>
            <td>{!!Form::checkbox('Max_check'.$value->id)!!}{!! Form::text('Max'.$value->id,$value->Max,['readonly'],array('size'=>'4'))!!}</td>
            <td>{!!Form::checkbox('Min_check'.$value->id)!!}{!!Form::text('Min'.$value->id, $value->Min,['readonly'],array('size'=>'4'))!!}</td>
          </tr>
        @endforeach
        </table>
        <div class="col-md-6 col-md-offset-11">
        {!!Form::submit('Save', array('class'=>'btn btn-primary'))!!}
        
		</div>
    </div>
    {!!Form::close()!!}
@endsection

Made changes in the names of all the form elements and I got all values in the request object.

Upvotes: 0

Andre
Andre

Reputation: 385

The problem is, that all checkboxes have the same name in the loop. You need something like this

{!!Form::checkbox('Field_check' . $key )!!}
{!!Form::checkbox('Max_check' . $key)!!}
{!!Form::checkbox('Min_check' . $key)!!}

Otherwise PHP can't tell you which row you edit.

Upvotes: 1

Related Questions