Home > Enterprise >  How to resolve Illegal string offset problem in laravel
How to resolve Illegal string offset problem in laravel

Time:12-03

I am a beginner at laravel development. I need to update the dynamic input field value. I tried so many codes but, I am unable to update. I am getting the error "Illegal string offset 'house_no'. I have given below using my code

My Blade File

<div>
    <table>
        <tr>
           <th>House No</th>
           <th>Street Name</th>
           <th>Area</th>
           <th>Pincode</th>
        </tr>
        @foreach ($address as $addres)
        <tr>
            <td><input type="text" name="settings[{{$addres->house_no}}]" value="{{ $addres->house_no}}"  class="form-control" /></td>
            <td><input type="text" name="settings[{{$addres->street_name}}]" value="{{ $addres->street_name}}"  class="form-control" /></td>
            <td><input type="text" name="settings[{{$addres->area}}]" value="{{ $addres->area}}"  class="form-control" /></td>
            <td><input type="text" name="settings[{{$addres->pincode}}]" value="{{ $addres->pincode}}"  class="form-control" /></td>
        <tr>
        @endforeach
    </table>
    </div>
</div>

My Controller File

public function updateUser(Request $request, $id)
{
    $user = $request->all();
    
    foreach ($request->settings as $key => $value) {
        Address::update([
            'house_no' => $value['house_no'],
            'street_name' => $value['street_name'],
            'area' => $value['area'],
            'pincode' => $value['pincode'],
        ]);
    }
}

CodePudding user response:

You would need to add an index to the array inputs you are creating where the key for the element is the id of the record (settings[key][attribute]). At the moment you have a single dimensional array that on the backside would end up only being 1 set of attributes for an address.

Assuming $addresses is an Eloquent Collection of models:

@foreach ($addresses as $key => $address)
<tr>
    <td><input type="text" name="settings[{{ $key }}][house_no]" value="{{ $address->house_no}}"  class="form-control" /></td>
    <td><input type="text" name="settings[{{ $key }}][street_name]" value="{{ $address->street_name}}"  class="form-control" /></td>
    <td><input type="text" name="settings[{{ $key }}][area]" value="{{ $address->area}}"  class="form-control" /></td>
    <td><input type="text" name="settings[{{ $key }}][pincode]" value="{{ $address->pincode}}"  class="form-control" /></td>
<tr>
@endforeach

Then your loop on the backside could iterate the 'sets' of settings:

foreach ($request->input('settings', []) as $key => $value) {
    Address::whereKey($key)->update(
        'house_no' => $value['house_no'],
        'street_name' => $value['street_name'],
        'area' => $value['area'],
        'pincode' => $value['pincode'],
    ]);
}

CodePudding user response:

you can update data using another way also like below sample example code

<?php 
    public function updateUser(Request $request, $id)
    {
        // print_r($request->all());
        $modelObj = User::where('id', '=', $id)->first();
        $modelObj->house_no = $request->house_no;
        $modelObj->street_name = $request->street_name;
        $modelObj->area = $request->area;
        $modelObj->pincode = $request->pincode;
        $modelObj->save(); // that line will update data
    }
?>

OR you can like this below

<?php 
    public function updateUser(Request $request, $id)
    {
        // print_r($request->all());
        $modelObj = User::where('id', '=', $id)->first();
        $modelObj->update($request->all());
    }
?>

If you wants to Modify Post or Get Request Input Data then you can

<?php 
$request->merge([
  'field_name' => fieldValue,
]);
?>

If you wants to Add field in Post or Get Request Input Data then you can

<?php 
    $request->request->add(['id' => $id]);
?>

or if you wants to remove field from Post or get Request input data the you can

<?php 
    $request->request->remove('key')
?>

for more detail about request data see here official docs https://laravel.com/docs/8.x/requests#retrieving-input

  • Related