I'm following the Laravel documentation to make this relationship with the id's type.
In the methods of the relationship I specified the pivot table and the IDS fields, however I was debugging the 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 do this code in tinker: PS: It instantiates the objects. / p>
$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 pivot (MemberGroups):
<?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();
}
}
Relationship model in the user model:
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 this based on the Laravel documentation: link