How to insert several records in the same table with the same name or id

0

Good morning classmates I have a question about how to insert this into a BD, as far as I understand it is with an array,

This is my form

<div class="panel-body">
  <form class="form-inline" method="POST" action="{{URL::action('UsuariosController@saveenc')}}">

    {{ csrf_field()}}
    <input type="text" id="id" name="id" value="{{ $id }}">
    <input type="text" name="pA" value="A" hidden>
    <div class="form-row">
      <table class="table table-striped">
        <p><b>A. Cuando se presenta un conflicto entre personas acerca de ideas. Tiendo a estar a favor de la parte que</b></p>

        <thead>
          <th></th>
          <th></th>
          <th></th>

        </thead>
        <tbody>
          <tr>
            <th scope="row">1</th>
            <td>Identifica e intenta sacar a la luz el conflicto</td>
            <td><input class="form-control" type="number" name="a[1a]" id="1a" required pattern="[0-9]" min="1" max="5"></td>
          </tr>
          <tr>
            <th scope="row">2</th>
            <td>Expresa de la mejor manera los valores e ideales involucrados</td>
            <td><input type="number" class="form-control" name="a[1b]" required pattern="[0-9]" min="1" max="5"></td>
          </tr>
          <tr>
            <th scope="row">3</th>
            <td>Refleja de la mejor manera mi opinión y experiencia personal</td>
            <td><input type="number" class="form-control" name="a[1c]" required pattern="[0-9]" min="1" max="5"></td>
          </tr>
          <tr>
            <th scope="row">4</th>
            <td>Se aproxima a la situación con la mayor lógica y consistencia</td>
            <td><input type="number" class="form-control" name="a[1d]" required pattern="^[0-9]+" min="1" max="5"></td>
          </tr>
          <tr>
            <th scope="row">5</th>
            <td>Mejor expresa el argumento de forma más concisa y contundente.</td>
            <td><input type="number" class="form-control" name="a[1e]" required pattern="^[0-9]+" min="1" max="5"></td>
          </tr>
        </tbody>

      </table>
    </div>
    <center><button class="btn btn-primary">Acceder</button></center>
  </form>

In my controller I have this part but it gives me the error of

  

A non-well-formed numeric value encountered

But it recognizes me as an array that part where I get an error in this way

array:5a  [▼
  "1a" => "1"
  "1b" => "2"
  "1c" => "3"
  "1d" => "4"
  "1e" => "5"
]

and This is the code I use to insert the values from my controller

public function saveenc(Request $request)
    {


      foreach($request->all() as $req){
        $encuesta = new Encuesta;
        $encuesta->id_usu = $request->get('id');
        $encuesta->pregunta = $request->get('pA');
        $encuesta->respuesta = $req['1a'];
        $encuesta->respuesta = $req['1b'];
        $encuesta->respuesta = $req['1c'];
        $encuesta->respuesta = $req['1d'];
        $encuesta->respuesta = $req['1e'];
        $encuesta->save();
        }
        return "exito";
    }

And I need it to be this way in the BD

-----------------------------------------
|id_enc | Pregunta | respuesta | id_usu |
-----------------------------------------
|  1    |    A     |1,4,3,2,5  |    1   |
-----------------------------------------

either that way or create a field for each answer.

I'm using Laravel 5.0 PHP 7.0, bootstrap 3.9

Following the suggestion of Andy Samuel, try

 $encuesta->respuesta = implode(',', $request->a);
 $encuesta->respuesta = implode(',', $request->a);

And now I get an SQL error

  

SQLSTATE [23000]: Integrity constraint violation: 1452 Can not add or update to child row: a foreign key constraint fails ( solvexintel . encuesta , CONSTRAINT encuesta_id_usu_foreign FOREIGN KEY ( id_usu ) REFERENCES usuario ( id_usu ) ON DELETE CASCADE) (SQL: insert into encuesta ( id_usu , pregunta , respuesta , updated_at , created_at ) values (60, A, 1,2,3, 4.5, 2018-08-28 18:00:46, 2018-08-28 18:00:46))

    
asked by Dohko19 28.08.2018 в 18:59
source

1 answer

0

Responding to my own question, the code stayed this way

        $encuesta = new Encuesta;
        $encuesta->id_usu = $request->get('id');
        $encuesta->pregunta = implode(',', $request->pa);
        $encuesta->respuesta = implode(',', $request->a);
        $encuesta->save();

This way I keep in the BD as requested, staying like this:

Thanks to andy for the tip

    
answered by 28.08.2018 / 20:34
source