PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, May 16, 2022

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

 May 16, 2022     laravel-7, mysql, php     No comments   

Issue

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 App\Http\Controllers\Auth\Client;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\Client;
use Illuminate\Http\Request;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Auth;

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');
    }

Solution

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');


Answered By - scorpions78
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing