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');
}
}