I'm having a hard time deciding between two different methods when processing data from a form in Laravel. Both methods are explained below:
1: Creating with Validated Data After Validation
$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);
2: Creating Directly Using request
$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']));
Which method is more secure and preferred?
Is there a performance difference between the two methods?
In general, in what situations would each method be recommended?
Thank you for your help!
there's a third option and it's better than these two
use the request form for validation why? here are a few reasons
and according to your question , the best way is to use validatedData
why?
Because you will be sure the data is already validated and it's not passed without any validation,
rather than that there's no difference