Relation Many to Many with id type UUID Laravel 5.4 Eloquent ORM

Asked

Viewed 276 times

0

I’m following Laravel’s documentation to make that relationship with the id type uuid. In the relationship methods I specified the pivot table and the IDS fields, however I went to debug in Tinker to see if it was really working and it returns me this PHP error: Call to undefined function ForcaVendas\Models\belongsToMany() in /Users/matheus/Documents/ForcaVendas/app/Models/Sistemas/Usuario.php on line 21 when I go to make this code in Tinker: PS:It instantiates the objects.

$usuario = ForcaVendas\Models\Usuario::find('um guid correspondente');
$grupoUsuario = new ForcaVendas\Models\GrupoUsuario;
$grupoUsuario->usuarios()->attach($usuario);

Migration user groups:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateGruposUsuariosTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('GruposUsuarios', function (Blueprint $table) {
            $table->uuid('GrupoUsuarioID')->primary();
            $table->string('Descricao', 100)->index();
            $table->text('Descritivo');
            $table->boolean('Status')->default(true);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::disableForeignKeyConstraints();
        Schema::dropIfExists('GruposUsuarios');
        Schema::enableForeignKeyConstraints();
    }
}

Migration users:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsuariosTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('Usuarios', function (Blueprint $table) {
            $table->uuid('UsuarioID')->primary();
            $table->string('Nome', 100);
            $table->string('Login', 100)->unique();
            $table->string('Senha', 100);
            $table->char('Genero', 1);
            $table->string('Email', 100);
            $table->boolean('Status')->default(true);
            $table->boolean('Administrador')->default(false);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::disableForeignKeyConstraints();
        Schema::dropIfExists('Usuarios');
        Schema::enableForeignKeyConstraints();
    }
}

Migration pivô (Gruposusuariosmembros):

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateGruposUsuariosMembrosTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('GruposUsuariosMembros', function (Blueprint $table) {
            $table->uuid('GrupoUsuarioMembroID');
            $table->uuid('GrupoUsuarioID')->index();
            $table->uuid('UsuarioID')->index();
            $table->foreign('GrupoUsuarioID')->references('GrupoUsuarioID')->on('GruposUsuarios')->onDelete('cascade');
            $table->foreign('UsuarioID')->references('UsuarioID')->on('Usuarios')->onDelete('cascade');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::disableForeignKeyConstraints();
        Schema::dropIfExists('GruposUsuariosMembros');
        Schema::enableForeignKeyConstraints();
    }
}

User model relationship method:

public function gruposUsuarios()
{
    return belongsToMany('ForcaVendas\Models\GrupoUsuario', 'GruposUsuariosMembros', 'UsuarioID', 'GrupoMembroID');
}

Relationship method in the user group model:

public function usuarios()
{
        return belongsToMany(Usuario::class, 'GruposUsuariosMembros', 'GrupoUsuarioID', 'UsuarioID');
}

I did it based on Laravel’s documentation: https://laravel.com/docs/5.4/eloquent-relationships#Many-to-Many

1 answer

0


There really is no such thing: ForcaVendas\Models\belongsToMany() at least in the package of just put this->belongsToMany and check the settings if they are correct, example:

Model Grupousuario

public function usuarios()
{
    return $this->belongsToMany(Usuario::class, 
              'GruposUsuariosMembros', 
              'GrupoUsuarioID', 
              'UsuarioID');
}

Model User

public function gruposUsuarios()
{
    return $this->belongsToMany(GrupoUsuario::class,
        'GruposUsuariosMembros', 
        'UsuarioID', 
        'GrupoUsuarioID');
}

Reference: Eloquent Relationships Many-to-Many

  • I’m sorry, I forgot $this->. Could you help me with another question? https://answall.com/questions/226294/autenticação-com-jwt-auth-e-dingo-api-laravel-5-4

Browser other questions tagged

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