Laravel 5.4 - Insert data from Droplist and Checkbox in BD

0

I already have the migrations of three tables, TABLE1: 'names' that contains 'id' and 'name'. TABLE 2: 'sports' containing 'id' and 'name', these two tables already contain names and sports. TABLE 3: 'ids' contains 'id', 'names_id' and 'sports_id', but it is empty since I want to fill it with the ids of the other two tables. Here is my code.

SelectController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Sport;
use App\Name;

class SelectController extends Controller
{
public function index()
{

  $sports = Sport::all();
  $names = Name::all();
  return view('select', ['sports'=> $sports], ['names'=>$names]);

}
public function store(Request $request)
{

    //
}
}

select.blade.php

<!DOCTYPE html>
<html>
<body>
  <title>Select name and sports</title>
<form action="/table" method="post">
  {{ csrf_field() }}
  Elija un nombre:<br>
  <select name="names">
    <option value="">Nombre</option>
  @foreach($names as $name)
    <option value="{{ $name->id }}">{{ $name->name}}</option>
  @endforeach
  </select><br><br>
  Elija los deportes que practica:<br>
  @foreach($sports as $sport)
  <input type="checkbox" class="form" name="sports" value="{{ $sport->id }}">{{ $sport->name }}<br>
  @endforeach
  <br><input type="submit" name="save" value="Guardar">
</form>
</body>
</html>

Migrations

Table 'names'

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateNamesTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('names', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('names');
}
}

Table 'sports'

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateSportsTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('sports', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('sports');
}
}

Table 'ids'

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateIdsTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('ids', function (Blueprint $table) {
        $table->increments('id');
        $table->string('names_id_');
        $table->string('sports_id');
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('ids');
}
}
    
asked by Paloma Alvarado 28.06.2017 в 22:08
source

1 answer

0

Well I have a way of doing it ... I do not know if it was the right one but it saves you for the time being.

<!DOCTYPE html>
<html>
<body>
  <title>Select name and sports</title>
<form action="/table" method="post">
  {{ csrf_field() }}
  Elija un nombre:<br>
  <select name="names">
    <option value="">Nombre</option>
  @foreach($names as $name)
    <option value="{{ $name->id }}">{{ $name->name}}</option>
  @endforeach
  </select><br><br>
  Elija los deportes que practica:<br>
  @foreach($sports as $sport)
  <input type="checkbox" class="form" name="sports[]" value="{{ $sport->id }}">{{ $sport->name }}<br>
  @endforeach
  <br><input type="submit" name="save" value="Guardar">
</form>
</body>
</html>

just change the name of sports to an array. Now on the route:

Route::post('table','TuControlador@laFuncion')

And inside the controller you do this:

public function laFuncion(Request $request){
    $names_id=$request->names;
    $sports_ids=$request->sports;
    foreach($sports_ids as $id){
        $ids=new TuModeloIds();
        $ids->names_id_=$names_id;
        $ids->sports_id=$id;
        $ids->save();
    }
    //hasta aqui todo guradado en adelante depende de ti
    return redirect()->back();
}

I hope I have been clear, and if it serves you, do not forget to mark it as answered

    
answered by 29.06.2017 / 22:13
source