Remove empty Request values from the form

Asked

Viewed 130 times

1

I need to remove the values that from the first input are empty.

    $count = count($request->item);

    for($i=0; $i < $count; $i++) {

        if ($request->item[$i] === null) {
            unset($request->item[$i]);
            unset($request->descricao[$i]);
            unset($request->quantidade[$i]);
            unset($request->valor_unitario[$i]);
            unset($request->desconto[$i]);
        }

    }
    dd($request->item);

is returning the following error:

"Indirect modification of overloaded Property App Http Requests Orcamentosrequest::$item has no Effect"

    <tr>
        <td><input id="item" name="item[]" type="text" class="form-control form-control-sm" aria-required="true" aria-invalid="false" value="{{ old('item.0') }}"></td>
        <td><input id="descricao" name="descricao[]" type="text" class="form-control form-control-sm" aria-required="true" aria-invalid="false" value="{{ old('descricao.0') }}"></td>
        <td><input id="quantidade" name="quantidade[]" type="text" class="form-control form-control-sm" aria-required="true" aria-invalid="false" value="{{ old('quantidade.0') }}"></td>
        <td><input id="valor_unitario" name="valor_unitario[]" type="text" class="form-control form-control-sm" aria-required="true" aria-invalid="false" value="{{ old('valor_unitario.0') }}"></td>
        <td><input id="desconto" name="desconto[]" type="text" class="form-control form-control-sm" aria-required="true" aria-invalid="false" value="{{ old('desconto.0') }}"></td>
        <td><a onclick="RemoveTableRow(this)" id="delete" class="delete"><i class="fa fa-trash"></i></a></td>
    </tr>

I’m getting the data all right, on request, only from the problem when using UNSET

  • Has the HTML part?

  • posted.. but it’s working.. only gives this error when I put the unset to remove

1 answer

1


I believe that the Request class does not allow direct modification in it. A possible solution is to do as follows:

        $count = count($request->item);

    for($i=0; $i < $count; $i++) {

        if ($request->item[$i] != null) {
            $item[]             = $request->item[$i];
            $descricao[]        = $request->descricao[$i];
            $quantidade[]       = $request->quantidade[$i];
            $valor_unitario[]   = $request->valor_unitario[$i];
            $desconto[]         = $request->desconto[$i];
            $orcamento_id       = $orcamento_id;
        }

    }
    dd($item);
  • tested bro, didn’t work, same error occurred

  • but request allows modification yes, but through "for" ta giving that stick in unset

  • or I can tell you shit too

  • i found a function that is $request->request->remove(value') but I don’t know how to specify which key it is from.. which is the item, but which key is.. in case it is an array

  • I searched the documentation and saw it too, has the replace function, maybe I can use it with the value removed, I’m coding an example for you to test

  • ball show

  • Analyzes this solution, the idea is to edit the item by creating a new array of items that do not contain the current null value.

  • "Undefined offset: 3"

  • 1

    you’re a genius bro.. gave me another idea.. just create another array, like the name of the key itself, and then I can discard the request.. bags

  • I was already doing it here, great that you got the vibe

  • It’s nois brother tamo together! vlw

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.