我想使用Laravel 5.7框架中的查询生成器在两个表中一起执行insertData。但是我发现输入错误,因此只能输入第一个数据表,而不能输入第二个表中的某些数据。请帮助解决此问题
我有两个桌子。第一个表是 Fruits Price Weight Number
1 Apples 20 2 10
2 Oranges 15 4 NA
3 Pineapple 40 NA 6
4 Avocado 60 5 20
表,之后,管理器表中的ID将被输入到Organizer
字段的Users
表中,并且userable_id
将被加载。 userable_type
OrganizerController.php
string: 'organizer'
OrganizerService.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;
}
在插入数据之前,先完成数据验证,以便仅输入 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
的ID没有以下消息
Organizer
如果我尝试删除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
我也尝试过仅使用Eloquent方法并将其混合,但仍未找到结果。请帮忙:)