Skip to content
Advertisement

How can I insert data in two tables at the same time during user registration in Laravel 7?

I have a registration form that will be used to register new users. My users are called “Clients” so I have two tables in mysql database (“clients” table and “users” table). I’m using Laravel 7. I would like to insert Clients data (first name, last name, email e.t.c) to both database tables at the same time during registration. I have created two Models which have a one-to-one relationship. I have also created a controller to register the Clients but at the moment the data is being inserted in one table (clients table). Please help me insert data in the two tables at the same time. Thanks.

RegisterController:

<?php

namespace AppHttpControllersAuthClient;

use AppHttpControllersController;
use AppModelsUser;
use AppModelsClient;
use IlluminateHttpRequest;
use IlluminateFoundationAuthRegistersUsers;
use IlluminateSupportFacadesHash;
use IlluminateSupportFacadesValidator;
use IlluminateSupportFacadesAuth;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    // Showing a Registration Form to User

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


    // Validating the User

    public function store(Request $request)
    {
        $request->validate([
            'first_name'            => 'required|string|max:255',
            'last_name'             => 'required|string|max:255',
            'telephone_number'      => 'required|digits:10',
            'email'                 => 'required|string|email|max:255|unique:users','regex:/^[w-.+]+@[a-zA-Z0-9.-]+.[a-zA-z0-9]{2,6}$/',
            'password'              => 'required|string|min:6|confirmed',
            'password_confirmation' => 'required',
        ]);

    // Creating the User

        $client = Client::create([
                'first_name'        => $request->first_name,
                'last_name'         => $request->last_name,
                'telephone_number'  => $request->telephone_number,
                'email'             => $request->email,
                'password'          => Hash::make($request->password),
        ]);
        
        return redirect('dashboard');
    }
      
}

Relationship in my User Model

public function client()
    {
        return $this->hasOne(Client::class,'user_id');
    }

Relationship in my Client Model

/**
     * @return BelongsTo
     */
    public function user(){
        return $this->belongsTo(User::class ,'user_id');
    }

Advertisement

Answer

If you want insert in other table and data it´s the same, maybe you can to do something like:

$otherName= OtherModel::create([    
    'first_name'        => $request->first_name,
    'last_name'         => $request->last_name,
    'telephone_number'  => $request->telephone_number,
    'email'             => $request->email,
    'password'          => Hash::make($request->password),
]);
        
return redirect('dashboard');
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement