Insert array in MySQL table with Laravel 5.4

0

I have a form like this:

What I want is that when selecting two or more sports, add a row for each sport with the id of the selected name, until now in my database is stored as follows:

And here is my Controller code:

$data = new Id;
  if(isset($_POST["save"])){
    $names = $_POST["names"];
    $sports =$_POST["sports"];
    $array = implode(', ', $sports);
    $data->name = $names;
    $data->sport = $array;
    $data->save();
    $ids = Id::all();
    return view('data', ['ids'=>$ids]);
  }
    
asked by Paloma Alvarado 30.06.2017 в 16:53
source

2 answers

0

A eloquent you can pass a multidimensional array to make multiple insert.

You can do something like this:

<?php
namespace App\Http\Controllers;

/* Incluye Query Builder */
use Illuminate\Support\Facades\DB;

/* Incluye Request */
use Illuminate\Http\Request;

/* Incluye tu modelo de Eloquent */
use App\MiModelo;

class MiclaseController extends Controller
{

    public function miMetodo(Request $request /* aqui recibes los datos*/ )
    {
        /* Se debe construir un array como este 
        $data = array(
            array('columna'=> 'valor', 'columna2'=> 'valor 2'), 
            array('name'=> 1, 'sport'=> 5),
            array('name'=> 1, 'sport'=> 8),

        );
        */

        /* declaramos el array */
        $datos_a_insertar = array();

        /* añadimos los datos al array */
        foreach ($request->sports as $key => $sport) 
        {
            $datos_a_insertar[$key]['name'] = $request->names;
            $datos_a_insertar[$key]['sport'] = $sport;
        }

        /* Luego simplemente lo insertamos con Eloquent o con Query Builder */
        /* con Eloquent */
        MiModelo::insert($datos_a_insertar); 
        /* o con Query Builder */
        DB::table('table')->insert($datos_a_insertar); 

        /* Retonamos la vista o lo que corresponda */
        return view('miVista');
    }


}

Certainly you do not need to use the $ _POST or $ _GET array directly, in fact it is inadvisable, because if you do so you will not be able to take advantage of the validation layers provided by laravel for example.

    
answered by 30.06.2017 / 18:05
source
0

I do not know Laravel, but you should do something like this:

if(isset($_POST["save"])){
    $names = $_POST["names"];
    $sports =$_POST["sports"];
    foreach($sports => $sport) {
        $data = new Id;
        $data->name = $names;
        $data->sport = $sport;
        $data->save();
    }    
    $ids = Id::all();
    return view('data', ['ids'=>$ids]);
  }

This is, instead of making an implode of the sports, you walk them with a for and insert them one by one.

However, if you want to save an id for each row, make sure that the data type of the table is int, and not varchar.

UPDATE: Also make sure that the checkboxes of the form in the html are of type array. That is, add the brackets at the end of the name. Something like this:

<input type="checkbox" name="sports[]" value="1"> Soccer
<input type="checkbox" name="sports[]" value="2"> Tochito

.... etc.

    
answered by 30.06.2017 в 17:22