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!