Hello! Three tables. users , verification_types , - stores id, name and slug. For example, id = 1, name = 'Verified', slug = 'verificated'. users_verifications - id , user_id (link with users`.`id ), verification_id (link with verification_types`.`id .

I want to, when registering a user, add a record to the users_verifications table by type: id = USER_ID, verification_id = 1 Is it possible to write data in parallel using table relations? There are migrations, in addition to the standard users table:

Table verification_types (Type verification levels: 1 = Guest, 2 = E-Mail confirmed, etc.)

 Schema::create('verification_types', function (Blueprint $table) { $table->increments('id'); $table->string('name')->comment('НазваниС уровня Π²Π΅Ρ€ΠΈΡ„ΠΈΠΊΠ°Ρ†ΠΈΠΈ'); $table->string('slug')->comment('БистСмноС Π½Π°Π·Π²Π°Π½ΠΈΠ΅'); $table->timestamps(); }); 

Table users_verifications (Verifications by type: user_id: 1 , verification_id: 3 = Confirmed number and E-Mail, etc.)

 Schema::create('users_verifications', function (Blueprint $table) { $table->increments('id'); $table->integer('user_id')->unsigned()->comment('ID ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Ρ'); $table->integer('verification_id')->unsigned()->comment('ID Ρ‚ΠΈΠΏΠ° Π²Π΅Ρ€ΠΈΡ„ΠΈΠΊΠ°Ρ†ΠΈΠΈ'); $table->timestamps(); $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); $table->foreign('verification_id')->references('id')->on('verification_types')->onDelete('cascade'); }); 

VerificationTypes Model

 class VerificationTypes extends Authenticatable { ... public function userVerification() { $this->belongsToMany('App/User'); } } 

User Model

 class User extends Authenticatable { use Notifiable; use HasRoleAndPermission; ... /** * User -> Π’ΠΈΠΏ Π²Π΅Ρ€ΠΈΡ„ΠΈΠΊΠ°Ρ†ΠΈΠΈ */ public function verificationStatus() { $this->belongsToMany('App\Entities\User\VerificationTypes', 'users_verifications', 'user_id', 'verification_id'); } } 

RegisterController controller

 class RegisterController extends Controller { ... protected function create(array $data) { $user = User::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); $role = Role::where('slug', '=', 'guest')->first(); // ΠŸΡ€ΠΈΠΊΡ€Π΅ΠΏΠ»ΡΠ΅ΠΌ Ρ€ΠΎΠ»ΡŒ ΠΊ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»ΡŽ $user->attachRole($role); /* * ΠŸΡ€ΠΈΠΊΡ€Π΅ΠΏΠ»ΡΠ΅ΠΌ ΡƒΡ€ΠΎΠ²Π΅Π½ΡŒ Π²Π΅Ρ€ΠΈΡ„ΠΈΠΊΠ°Ρ†ΠΈΠΈ */ $user->verificationStatus()->attach([$user->id, 1]); return $user; } } 

Thank you in advance!

    1 answer 1

    Fixed! The error was that I forgot in the return models.

    Then, two entries were added to the table. id=USER_ID, verification_id=1 and id=USER_ID, verification_id=USER_ID Then just instead

     $user->verificationStatus()->attach([$user->id, 1]); 

    Made:

     $user->verificationStatus()->attach([1]);