How to save in multiple tables Laravel

11
February 11, 2019, at 1:30 PM

I want to do insertData in both tables together using the Query Builder in the Laravel 5.7 framework. But I found an input error so that only the first table of data can enter and some data in the second table cannot enter. Please help to resolve this problem

I have two tables. The first table is the Organizer table, where later the ID in the Organizer table will be entered into the Users table in the userable_id field and userable_type will be loaded with the string: 'organizer'

OrganizerController.php

public function store(Request $request)
{
    $request->validate([
        'identity_id' => 'required|string|unique:organizers',
        'full_name' => 'required|string|max:191',
        'address' => 'required|string',
        'photo' => 'required',
        'email' => 'required|string|email|max:191|unique:users',
        'password' => 'required|string',
        'home_phone' => 'numeric',
        'mobile_phone' => 'numeric',
        'line_id' => 'string',
        'facebook_id' => 'string',
        'instagram_id' => 'string',
        'website_link' => 'string',
        'status' => 'required|boolean',
        'userable_id' => 'required|numeric',
        'userable_type' => 'required|string'
    ]);
    $result = $this->organizerService->postData($request);
    return $result;
}

OrganizerService.php

    public function postData($params)
    {
        try {
            if (isset($params['photo']) && $params['photo'] != '') {
                $photoName = 'organizers_' . $params['identity_id'] . '_' . date('YmdHis') . '.png';
                $photoPath = Organizer::PhotoDir;
                $imgPath = move_uploaded_file($photoName, public_path() . '/app/' . $photoPath);
                unlink($imgPath);
            }
            $organizers = Organizer::create([
                'identity_id' => $params['identity_id'],
                'full_name' => $params['full_name'],
                'address' => $params['address'],
                'photo' => $photoName,
                'home_phone' => $params['home_phone'],
                'mobile_phone' => $params['mobile_phone'],
                'line_id' => $params['line_id'],
                'instagram_id' => $params['instagram_id'],
                'facebook_id' => $params['facebook_id'],
                'website_link' => $params['website_link'],
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s')
            ])->save();
            if (isset($params['userable_type']) && $params['userable_type'] != '') {
                $userableType = 'organizers';
            }
            if (isset($params['userable_id']) && $params['userable_id'] != '') {
                $userableId = $organizers->id;
            }
            DB::table('users')->insert([
                'email' => $params['email'],
                'password' => Hash::make($params['password']),
                'userable_id' => $userableId,
                'userable_type' => $userableType,
                'status' => $params['status'],
                'created_at' => $organizers->created_at,
                'updated_at' => $organizers->updated_at
            ]);
            $this->response['status'] = self::SUCCESS_STATUS;
            $this->response['message'] = self::SUCCESS_MESSAGE;
            $this->response['result'] = $params;
        } catch (\Exception $e) {
            $this->response['result'] = $params;
            $log = ['Service' => 'OrganizerService', 'function' => 'postDataOrganizer', 'params' => $params];
//            logError($e, $log);
        }
        return $this->response['result'];
    }

Before inserting data data validation is done so that only userable_id data is not input. After I did insert data, to field userable_id not have an id of the table Organizer with the following message

errors: {userable_id: ["The userable id field is required."]}
userable_id: ["The userable id field is required."]
0: "The userable id field is required."
message: "The given data was invalid."

If I try to remove userable_id validation, the insert results are accepted like this

{
address: "1"
created_at: null
email: "1@mail.com"
facebook_id: "1"
full_name: "1"
home_phone: 1
id: null
identity_id: "1111111"
instagram_id: "1"
line_id: "1"
mobile_phone: 1
password: "11111111111"
photo:'test.jpg'
status: "1"
updated_at: null
userable_id: null
userable_type: "organizer"
website_link: "1"
}

I have also tried using Eloquent method only and mixing it but still not found results. Please help :)

READ ALSO
fetch data from sql into php array returns nothing

fetch data from sql into php array returns nothing

I'm trying to json_encode the data that was fetched from SQL, however when i stored it into PHP array, it didn't return any valueIs there any code that i am missing or are there limits to the array?

20
How to have recursive tree genealogy in symfony

How to have recursive tree genealogy in symfony

I'm making tree genealogy in symfony

10
PHP Subscribe Form with Email Duplicate Check

PHP Subscribe Form with Email Duplicate Check

I'm very new to PHP and struggling with adding in duplicate email check with this code

24
Send Message to Telegram Bot Without Open Telegram Apps

Send Message to Telegram Bot Without Open Telegram Apps

usually if we want to chat or send command to Telegram Bot, we just texting the bot from Telegram apps (desktop, web, or smartphone)Is there any way texting the bot without open Telegram apps?? I've read many website and thread in stackoverflow, but no clue

43