如何在多个表中保存Laravel

问题描述 投票:0回答:1

我想使用Laravel 5.7框架中的查询生成器在两个表中一起做insertData。但是我发现输入错误,因此只能输入第一个数据表,而不能输入第二个表中的某些数据。请帮助解决此问题

我有两个桌子。第一个表是Organizer表,之后,管理器表中的ID将被输入到Users字段的userable_id表中,并且userable_type将被加载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'];
    }

在插入数据之前进行数据验证,以便仅输入userable_id数据。在我插入数据之后,到字段userable_id的表Organizer的ID没有以下消息

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."

如果我尝试删除userable_id验证,则接受插入结果是这样的>>

{
address: "1"
created_at: null
email: "[email protected]"
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"
}

我也尝试仅使用Eloquent方法并将其混合,但仍未找到结果。请帮助:)

我想使用Laravel 5.7框架中的查询生成器在两个表中一起做insertData。但是我发现一个输入错误,因此只能输入第一个数据表,而某些数据在...

php laravel eloquent laravel-query-builder
1个回答
0
投票

我有一个包含申请人数据的申请表。但是我想将数据插入到students_info,students_records和students_contacts三个表中当用户批准申请表中的记录时students_info id是两个表中的外键

© www.soinside.com 2019 - 2024. All rights reserved.