Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dx7's avatar
Level 1

Split user data into two tables

Well, I want to divide the data used in two tables, where the user table contains the email and password, and the second table has data such as address, name and phone number. It did not work for me, why I do not know I use a library laratrust

table " class CreateLawdatasTable extends Migration {

public function up()
{
    Schema::create('lawdatas', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->bigIncrements('id');
        $table->unsignedBigInteger('user_id');
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->unsignedBigInteger('role_id');
        $table->foreign('role_id')->references('id')->on('roles')
        ->onUpdate('cascade')->onDelete('cascade');
        $table->string('phone');
        $table->string('address');
        $table->string('signature');
        $table->timestamps();
    });
}


public function down()
{
    Schema::dropIfExists('lawdatas');
}

} " Relationship " public function Lawdata() { return $this->belongsToMany('user_id'); }

} " model " class Lawdata extends Model { use HasFactory; use LaratrustUserTrait;

protected $table ='lawdatas';



protected $fillable = [
   
    'address' ,
    'phone'   ,
    'signature',
    
];
public function User()
{
    return $this->hasMany('App\User');
}

} " laratrust table " class LaratrustSetupTables extends Migration {

public function up()
{
    // Create table for storing roles
    Schema::create('roles', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name')->unique();
        $table->string('display_name')->nullable();
        $table->string('description')->nullable();
        $table->timestamps();
    });

    // Create table for storing permissions
    Schema::create('permissions', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name')->unique();
        $table->string('display_name')->nullable();
        $table->string('description')->nullable();
        $table->timestamps();
    });

    // Create table for associating roles to users and teams (Many To Many Polymorphic)
    Schema::create('role_user', function (Blueprint $table) {
        $table->unsignedBigInteger('role_id');
        $table->unsignedBigInteger('user_id');
        $table->string('user_type');

        $table->foreign('role_id')->references('id')->on('roles')
            ->onUpdate('cascade')->onDelete('cascade');

        $table->primary(['user_id', 'role_id', 'user_type']);
    });

    // Create table for associating permissions to users (Many To Many Polymorphic)
    Schema::create('permission_user', function (Blueprint $table) {
        $table->unsignedBigInteger('permission_id');
        $table->unsignedBigInteger('user_id');
        $table->string('user_type');

        $table->foreign('permission_id')->references('id')->on('permissions')
            ->onUpdate('cascade')->onDelete('cascade');

        $table->primary(['user_id', 'permission_id', 'user_type']);
    });

    // Create table for associating permissions to roles (Many-to-Many)
    Schema::create('permission_role', function (Blueprint $table) {
        $table->unsignedBigInteger('permission_id');
        $table->unsignedBigInteger('role_id');

        $table->foreign('permission_id')->references('id')->on('permissions')
            ->onUpdate('cascade')->onDelete('cascade');
        $table->foreign('role_id')->references('id')->on('roles')
            ->onUpdate('cascade')->onDelete('cascade');

        $table->primary(['permission_id', 'role_id']);
    });
}

" RegisteredUserController " class RegisteredUserController extends Controller {

public function create()
{
    return view('auth.register');
}


public function store(Request $request)
{
    $request->validate([
        'name' => ['required', 'string', 'max:255'],
        'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        'password' => ['required', 'confirmed', Rules\Password::defaults()],
    


    ]);

    $user = User::create([
        'name' => $request->name,
        'email' => $request->email,
        'password' => Hash::make($request->password),
    
    ]);
    $user->attachRole($request->role_id);
    event(new Registered($user));

    Auth::login($user);

    return redirect(RouteServiceProvider::HOME);
}

} "

0 likes
3 replies
Snapey's avatar

Why did it not work for you?

Add user_id column to the 'profile' table and setup a hasOne relationship on the User model and a belongsTo relationship on the Profile.

If you want help with your actual code, actually show it.

dx7's avatar
Level 1

@Snapey Well I did as you asked me to help me please

Snapey's avatar

@dx7 method names should always start with a lower case letter

user model should have

public function lawdata()
{
	return $this->hasOne(Lawdata::class);
}

Lawdata model should have

public function user
{
	return $this->belongsTo(User::class)
}

Sort out your relationships and then test with tinker.

1 like

Please or to participate in this conversation.