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

Prathamesh6921's avatar

Store data in 3 tables in

Hi all,i new here and in laravel also,i want to store data in 3 tables using single registration form

class UserController extends Controller { public function registerLawyer(Request $request){ $firstname = $request->get('firstname'); $lastname = $request->get('lastname'); $country = $request->get('country'); $role = $request->get('role'); $dob = $request->get('dob'); $email = $request->get('email'); $pass = $request->get('password'); $aop = $request->get('pracitces'); $licensenum = $request->get('licenseNumber'); $license = $request->get('license');

    $user_data = DB::table('users')->insertGetId([
        'firstname' => $firstname,
        'lastname' => $lastname,
        'country' => $country,
        'role' => $role,
        'dob' => $dob,
        'email' => $email,
        'password' => $pass,
    ]);
    $user = DB::table('users')->where(['firstname'=>$firstname])->first('id');
    $lawyer_data = DB::table('lawyer')->insertGetId([
        'u_id' => $user->id,
        'practices' => $aop
    ]);
    $law = DB::table('lawyer')->where(['u_id'=>$user->id])->get('id');
    $license_data = DB::table('license')->insert([
            'lawyer_id' => $law->id,
            'licenseno' => $licensenum,
            'institution' => $license
        ]);
        return redirect('welcome');
            }

}

('id' is a primary key in user table) "u_id" is foreign key in 'lawyer' table and lawyer_id is a foreign key in license table. While storing the data i am getting error,

Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_RECOVERABLE_ERROR) Argument 1 passed to Illuminate\Database\Grammar::columnize() must be of the type array, string given, called in C:\Users\Syntag\phpProjects\quesmi_web\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php on line 137

i have written all the code in controller only ,i am not using model or eloquent,is there any way to get this done.

0 likes
1 reply
andreich1980's avatar
Level 49
  1. Please, reformat your code like described here https://help.github.com/articles/creating-and-highlighting-code-blocks/

  2. You would use Eloquent Models and set up relationships. Then everything will work as a charm.

// User.php
public function lawyers() {
    return $this->hasMany(Lawyer::class);
    // or hasOne? whatever relation you have got there
}

// Lawyer.php
public function user() {
    return $this->belongsTo(User::class);
}

// License.php
public function lawyer() {
    return $this->belongsTo(Lawyer::class);
}

// UsersController.php
public function registerLawyer(Request $request){
    $user_attributes = $request->only([
        'firstname', 
        'lastname',
        // all the attributes you need for the user
    ]);
    
    // Or you would even like to validate the attributes 
    //$user_attributes = $request->validate(array_or_rules);
    
    $user = User::create($user_attributes)
    
    $lawyer_attributes = $request->only([
        'practices', 
    ]);
    // Or you would even like to validate the attributes 
    //$lawyer_attributes = $request->validate(array_or_rules);
    
    $lawyer = $user->lawyers()->save(new Lawyer($lawyer_attributes));
    
    $license_attributes = $request->only([
        'licenseno', 
        'institution',
    ]);
    // Or you would even like to validate the attributes 
    //$license_attributes = $request->validate(array_or_rules);
    
    $license = $lawyer ->licenses()->save(new License($license_attributes));
    
    return redirect('welcome');
}

Read about validation https://laravel.com/docs/5.6/validation, Eloquent https://laravel.com/docs/5.6/eloquent

1 like

Please or to participate in this conversation.