I can not save all the foreach log - LARAVEL

-1

In user id it has 2 record only that at save time it only saves 1 record and not 2, how should I do to save correctly?

Follow Code below

Controller Lists user data

public function listardados(){
    $matricula = Matricula::where('user_id', Auth::id())->get();

    //dd($matricula);
    return view('dashboard.renovacao.teste', compact( 'matricula'));
}

- Controller Saves form registration

public function store(RenovacaoRequest $request){


    $user = Auth()->user();

    $dados = $request->all();

    $renovacao = Renovacao::create($dados);


    return view('dashboard.renovacao.confirmacao', compact ('renovacao'));
}

- View form

@extends('layouts.app') @section('content')

<div class="container">
    <div class="row">

        <form class="form-horizontal " id="regForm" action="{{route('renovacao.store')}}" method="POST">
            <div class="card-panel white">
                <h4 class="center">Solicitar Renovação</h4>
                <div class="row"></div>
                {{ csrf_field()}}
                <right>
                    <a>**Dados Cadastrados**</a>
                </right>
                <div class="row"></div>
                <div class="row"></div>
                <div class="row">
                    @foreach($matricula as $matric)
                    <div class="row">
                        <div class="col s6 m6">
                        <div class="input-field {{$errors->has('user_id') ? 'has-error' : ''}} ">
                            <label for="produto">Nome do Pai:</label>
                            <input type="text" class="form-control" name="nomerespo[]" value="{{ $matric->nomedopai }}">

                        </div>
                        </div>

                        <div class="col s6 m6">
                        <div class="input-field {{$errors->has('user_id') ? 'has-error' : ''}} ">
                            <label for="produto">Nome do Aluno(a):</label>
                            <input type="text" class="form-control" name="nomealuno[]" value="{{ $matric->nomealuno }}">    
                        </div>
                        </div>

                </div>
                @endforeach

                <div class = "row">
                    <div class="col s12">

                        <a title="Voltar Para Página Principal" class="btn orange darken-4 btn-info left " href="/admin">Voltar
                            <i class="material-icons left">arrow_back_ios</i>
                        </a>

                        <button type="submit" class="btn orange darken-4 btn-info right">Confirmar
                            <i class="material-icons left">save</i>
                        </button>
                    </div>
                </div>
            </div>
        </div>    
    </form>

</div>

@endsection

    
asked by anonymous 22.11.2018 / 19:47

1 answer

0

Hello, the input tags on your form have a fixed name inside a @foreach , so when sending a form with multiple entries only one (of each name) is actually sent.

Try changing the following:

<input type="text" class="form-control" name="nomerespo" value="{{ $matric->nomedopai }}">
<input type="text" class="form-control" name="nomealuno" value="{{ $matric->nomealuno }}">

To:

<input type="text" class="form-control" name="nomerespo[]" value="{{ $matric->nomedopai }}">
<input type="text" class="form-control" name="nomealuno[]" value="{{ $matric->nomealuno }}">

Notice that the name attributes of the tags have been changed to array , so all fields will be sent to your Controller

If the relationship with Renovacao with user has been set and the $guarded or $fillable property is set to Renovacao you can save content with something like: $user->renovacoes()->saveMany($dados) and Laravel will deal with the foreign keys for you.

Edit

The for loop of your form would look like this:

@foreach($matricula as $matric)
    <div class="row">
        <div class="col s6 m6">
        <div class="input-field {{$errors->has('user_id') ? 'has-error' : ''}} ">
            <label for="produto">Nome do Pai:</label>
            <input type="text" class="form-control" name="nomerespo[]" value="{{ $matric->nomedopai }}">

        </div>
        </div>

        <div class="col s6 m6">
        <div class="input-field {{$errors->has('user_id') ? 'has-error' : ''}} ">
            <label for="produto">Nome do Aluno(a):</label>
            <input type="text" class="form-control" name="nomealuno[]" value="{{ $matric->nomealuno  }}">
        </div>
        </div>

    </div>
@endforeach

The dd($request->all()) should return the following:

FromyourcommentsandtheprintsInoticedthatthereisnorelationshipestablishedinyourModelUserwiththerenovacaoIusedasanexample,soitisnotpossibletocreatewith$user->renovacao()->createMany()oranyotheroperationrelatedto$user->renovacao().

Edit2

Tomakeitdocumented,thearraywasnotappearinginitsddbecauseitfailedvalidation.SinceHTMLhasbeenchangedtosendanarrayitisnecessarytochangethevalidationrulesaswell:

['nomerespo.*'=>'required|string|max:255','nomealuno.*'=>'required|string|max:255',]

Thiscodevalidatestheinputasanarray.TousethecreatemethodasyouwantitisnecessarytochangetheHTMLagain:

<inputtype="text" class="form-control" name="rematricula[{{ $loop->index}}][nomerespo]" value="{{ $matric->nomedopai }}">
<input type="text" class="form-control" name="rematricula[{{ $loop->index}}][nomealuno]" value="{{ $matric->nomealuno }}">

Notice that the input name has been changed to merge and there is one more level in the array.

Now in your Contoller can do something like:

$dados = $request->get('rematricula');

foreach ($dados as $key => $dado) {
    Rematricula::create($dado);
}

I suggest checking the input the Controller is receiving and looking at the Laravel documentation ( link ) for better understand the code.

    
25.11.2018 / 00:39