4
I have a form that returns an array with the values:
Número: {{Form::text('tel[]')}}
Proprietário: {{Form::select('proprietario[]',['Aluno'=>'Aluno','Pai'=>'Pai'])}}
Tipo: {{Form::select('tipo[]'['Pessoal'=>'Pessoal', 'Comercial'=>'Comercial'])}}
I’m trying to put it in the bank like this:
    $telefone=$_POST['tel'];    
    $proprietario=$_POST['proprietario'];
    $tipo=$_POST['tipo'];
    if(count($telefone)>0){
        for ($i = 0; $i < count($telefone); $i++) {
            $tel->numero=$telefone[$i];
            $tel->proprietario=$proprietario[$i];
            $tel->tipo=$tipo[$i];
        }
    }
The problem is that it is only inserting the last value of the array. What I am doing wrong?
See if in the generated html the
namethis one with clasps.– rray
Yes, it has clasps.
– Amanda Lima
you saw it by
ctrl+ubrowser?– rray
Yes. Displaying the page’s source code
– Amanda Lima
you already did
dd($telefone)to see what is stored?– RFL
Already, it is storing the data correctly in the variable. The impression I have, is that the
fordoes not traverse the array, but I gave aechoin the$iand it’s okay.– Amanda Lima
What is the array of your entire collection, can make a
echo '<pre>' . print_r($_POST); die();?– Ivan Ferrer
Yes, I can. The data is being captured by
$_POSTcorrectly.– Amanda Lima
tries to put a
$tel->save()at the end offoreach– RFL
You already have @Rafaelacioly
– Amanda Lima
@Rafaelacioly, inverti and still keeps inserting only the last record :(
– Amanda Lima
@Amandalima seeks help in the community
slackthere for sure they can help you! https://laravel-br.slack.com/– RFL
It’s kind of weird!
$_POSTin Laravel?!?!? would not beInput::get?– Wallace Maxters
$tel->tipo=$tipo[$i];the equals signal for a normal variable within a loop, stores only one value, and they are being replaced until the last one arrives, so you only get the references of the last index of the traversed array.– Edilson