1

In a Laravel model, I am using a JSON column data to store custom data. The JSON data can be nested for organization, and all keys should have non-empty/non-null values.

At the moment all keys are hard-coded in the form as data[key] on the frontend. If a key previously stored has its value blanked out on the frontend, key should be unset in the update method. However, upon validation, I'm getting the original array back.

// ModelController.php
public function update(Request $request, Model $model)
{
    // $request->data = ['key' => null, 'nested' => ['key' => null]]

    function stripEmptyCustom($data) {
        foreach ($data as $key => $value) {
            if (is_array($data[$key])){
                $data[$key] = stripEmptyCustom($data[$key]);
            }

            if (empty($value)){
                unset($data[$key]);
            }
        }

        return $data;
    }

    $request->data = stripEmptyCustom($request->data);

    dump($request->data);                   // so far, so good
                                            // $request->data = []

    $attributes = $request->validate([
        'name' => 'nullable',
        'data' => 'array',
    ]);

    dump($attributes);                      // $attributes['data'] =
                                            // ['key' => null, 'nested' => ['key' => null]]

How do I validate the request without it resetting the original data?

Erich
  • 2,408
  • 18
  • 40
  • [array_filter](https://www.php.net/manual/en/function.array-filter.php) can be used to remove keys with a null/empty value. – Brian Lee Feb 12 '20 at 21:46
  • @DigitalDrifter I read that too, however `$data = array_filter($request->data)` was leaving the keys in there. as was `array_filter($request->data, 'array_filter')` – Erich Feb 12 '20 at 21:54
  • Something else is likely off then, I've used it before this exact purpose. As stated by the docs: `If no callback is supplied, all entries of array equal to FALSE (see converting to boolean) will be removed.` – Brian Lee Feb 12 '20 at 22:10
  • there's that too. but since my code up to that point demonstrably works, I'm leaning towards the issue being within the `$request` itself. like you know how there's an `original` array and an `attributes` array within each model? wondering if my update changes one, but the other's being validated. – Erich Feb 12 '20 at 22:20

2 Answers2

0

I think you want to only return the validated data, and not the whole data response. The validation method will continue the controller normally if the validated data is correct, It might be doing another request in there somewhere.

what I think you are looking for is to only return the data that is valid:

using something like: $request->validated(); hopefully will solve your issue:

Im referencing these resources: How do I get ONLY the validated data from a laravel FormRequest?

validation docs: https://laravel.com/docs/5.6/validation

Sweet Chilly Philly
  • 3,014
  • 2
  • 27
  • 37
0

I found a workaround in stripping the nulls after validation:

function stripEmptyCustom($data) {
    foreach ($data as $key => $value) {
        if (is_array($data[$key])){
            $data[$key] = stripEmptyCustom($data[$key]);
        }

        if (empty($value)){
            unset($data[$key]);
        }
    }

    return $data;
}

$attributes = $request->validate([
    'name' => 'nullable',
    'data' => 'array',
]);

$attributes = stripEmptyCustom($attributes);
Erich
  • 2,408
  • 18
  • 40