Relationship Doubt - Laravel?

Asked

Viewed 61 times

1

I’m new to Laravel and PHP and I’m trying to create a simple voting system. It turns out, I’m having doubts about how to relate between my tables.

For example:

I have the table of Pessoa and the table of Projeto, both contain only id and nome. And I also created the votação, where it will contain the person, project, and voting id. So 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 doubt how the right way to relate these models with the Eloquent.

1 answer

0


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

    Thanks! You helped me a lot. I managed to realize the relationship and understand better. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.