Example
I would use it that way with model
Votacao
Migrations
Projects
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class Projetos extends Migration
{
public function up()
{
Schema::create('projetos', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 50);
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('projetos');
}
}
People
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class Pessoa extends Migration
{
public function up()
{
Schema::create('pessoa', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 50);
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('pessoa');
}
}
Voting
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class Votacao extends Migration
{
public function up()
{
Schema::create('votacao', function (Blueprint $table) {
$table->increments('id');
$table->integer('projeto_id')->unsigned();
$table->integer('pessoa_id')->unsigned();
$table->enum('voto', ['sim', 'nao', 'abstencao']);
$table->timestamps();
$table->foreign('projeto_id')->references('id')->on('projetos');
$table->foreign('pessoa_id')->references('id')->on('pessoa');
});
}
public function down()
{
Schema::dropIfExists('votacao');
}
}
Models
Projects
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Projetos extends Model
{
protected $fillable = ['name'];
protected $table = "projetos";
protected $primaryKey = "id";
public function votacao()
{
return $this->hasMany(Votacao::class,"projeto_id","id");
}
}
Person
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Pessoa extends Model
{
protected $fillable = ['name'];
protected $table = "pessoa";
protected $primaryKey = "id";
public function votacao()
{
return $this->hasMany(Votacao::class,"pessoa_id","id");
}
}
Voting
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Votacao extends Model
{
protected $fillable = ['voto','projeto_id', 'pessoa_id'];
protected $table = "votacao";
protected $primaryKey = "id";
public function pessoa()
{
return $this->belongsTo(Pessoa::class, "pessoa_id", "id");
}
public function projeto()
{
return $this->belongsTo(Projetos::class, "projeto_id", "id");
}
}
Thanks! You helped me a lot. I managed to realize the relationship and understand better. Thank you!
– Felipe Ramon