phplaravelvalidationrequestcontroller

Validation and Data Generation Methods in Laravel: Which Method is Better?


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']));

My Questions:

Thank you for your help!


Solution

  • there's a third option and it's better than these two

    use the request form for validation why? here are a few reasons

    1. the data validated before reaching to the controller
    2. it's cleaner if the validation separated from the logic
    3. it's following the solid principles

    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