samedi 27 juillet 2019

how to insert data in child and parent table at single query in laravel

I am try to insert child and parent table record at single form. e.g: child table: profile parent table user

   both connected one to one relationship 

user model : profile in belongTo(ProfileModels::class)

profile model : user in belongTo(User::class)

I have try to insert both table user and profile at one single user interface form to submit value in Request object. Get all value in controller and insert use User::create() laravel insert function.

controller:

protected function register(Request $data)
{
    /*$data = [
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
        'profiles.first_name' => $data['profiles.first_name'],
        'profiles.last_name' => $data['profiles.last_name'],
        'profiles.gender' => $data['profiles.gender'],
        'profiles.mobile_no' => $data['profiles.mobile_no'],
        'profiles.address' => $data['profiles.address'],
        'profiles.birthdate' => $data['profiles.birthdate'],
    ];*/

        $user = User::create($data);
        if($user){
            $user->profiles()->create($data['profiles']);
        }


}

Expected : insert both table record at single insert query. Actual : error and not insert table record



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2SIQiTR
via IFTTT

Aucun commentaire:

Enregistrer un commentaire