Doubt with Relationship - Laravel?

1

I'm new with Laravel and PHP and I'm trying to create a simple voting system. It turns out that, I am in doubt as to how to relate between my tables.

For example:

I have the Pessoa table and the Projeto table, both contain only id and nome . And I also created the votação table, which will contain the id of the person, the project, and the voting. Thus is the migration of votação :

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

    });
}

I'm wondering how the correct way to relate these models to Eloquent would be.

    
asked by anonymous 30.03.2017 / 20:08

1 answer

0

Example

I would use this 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');
    }
}

Vote

<?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");
    }
}

Vote

<?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");
    }
}
    
31.03.2017 / 02:32