1: Создание с проверенными данными после проверки
Код: Выделить всё
$validatedData = $request->validate([
'customer_id' => 'required|integer|exists:users,id',
'request_id' => 'required|integer|exists:requests,id',
'addressInfo.city' => 'required|string|max:255',
'addressInfo.district' => 'required|string|max:255',
'addressInfo.neighborhood' => 'required|string|max:255',
'addressInfo.addressDetail' => 'nullable|string|max:500',
'description' => 'required|string|max:500',
'files.*' => 'nullable|file|mimes:jpeg,png,jpg,pdf|max:2048',
]);
$company = getCurrentCompany();
$customer = $company->customers()->findOrFail($validatedData['customer_id']);
$request = $customer->requests()->findOrFail($validatedData['request_id']);
$photos = [];
// File upload processing
foreach ($request->files ?? [] as $index => $file) {
if ($request->hasFile('files.' . $index)) {
$photos[] = [
'index' => $index,
'file' => $request->file('files.' . $index)->store('keyRequests/photos')
];
}
}
// Prepare extra data
$validatedData['photos'] = $photos;
$validatedData['requester_model'] = 'App\Models\Company';
$validatedData['status'] = KeyRequest::getStatuses()[0];
$validatedData['company_id'] = $company->id;
$validatedData['request_address_id'] = $request->address->id;
$validatedData['deed_id'] = $request->deed->id;
// Create key request
$createData = Arr::except($validatedData, ['confirmation', 'files', 'redirectToEdit']);
$keyRequest = $customer->keyRequests()->create($createData);
Код: Выделить всё
$validatedData = $request->validate([
'customer_id' => 'required|integer|exists:users,id',
'request_id' => 'required|integer|exists:requests,id',
'addressInfo.city' => 'required|string|max:255',
'addressInfo.district' => 'required|string|max:255',
'addressInfo.neighborhood' => 'required|string|max:255',
'addressInfo.addressDetail' => 'nullable|string|max:500',
'description' => 'required|string|max:500',
'files.*' => 'nullable|file|mimes:jpeg,png,jpg,pdf|max:2048',
]);
$company = getCurrentCompany();
$customer = $company->customers()->findOrFail($validatedData['customer_id']);
$request = $customer->requests()->findOrFail($validatedData['request_id']);
$photos = [];
// File upload processing
foreach ($request->files ?? [] as $index => $file) {
if ($request->hasFile('files.' . $index)) {
$photos[] = [
'index' => $index,
'file' => $request->file('files.' . $index)->store('keyRequests/photos')
];
}
}
// Prepare extra data
$request['photos'] = $photos;
$request['requester_model'] = 'App\Models\Company';
$request['status'] = KeyRequest::getStatuses()[0];
$request['company_id'] = $company->id;
$request['request_address_id'] = $request->address->id;
$request['deed_id'] = $request->deed->id;
// Create key request
$keyRequest = $customer->keyRequests()->create($request->except(['confirmation', 'files', 'redirectToEdit']));
- Какой метод более безопасен и предпочтителен?
- Есть ли разница в производительности между этими двумя методами?
- В целом, в каких ситуациях рекомендуется использовать тот или иной метод?
Подробнее здесь: https://stackoverflow.com/questions/791 ... -is-better