Ya tengo las migraciones de tres tablas, TABLA1: 'names' que contiene 'id' y 'name'. TABLA2: 'sports' que contiene 'id' y 'name', estas dos tablas ya contienen nombres y deportes. TABLA3: 'ids' contiene 'id', 'names_id' y 'sports_id', pero está vacía ya que quiero llenarla con los ids de las otras dos tablas. Aquí está mi código.
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>
Migraciones
Tabla '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');
}
}
Tabla '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');
}
}
Tabla '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');
}
}