1

Note: Storing 'details' as json field in database

Form Request Validation Code:

 public function rules(Request $request)
  {
    return [
      'name' => 'required | string',
      'details.subscriptions' => 'required | array',
      'details.subscriptions.*' => 'nullable | string',
      'details.size' => 'nullable | array',
      'details.size.*' => 'nullable | string',
      'details.notes' => 'nullable | array',
      'details.notes.*' => 'nullable | string',
      'details.price' => 'nullable | array',
      'details.price.*' => 'nullable | double',
    ];
}
protected function prepareForValidation()
  {
    $this->merge([
      'name' => ucwords($this->name),
    ]);
  }

Validated result - what I get:

{
"name":"Some Name",
"details":{
          "subscriptions":["1"],
          "size":[null,null],
          "notes":["10 pcs",null],
          "price":[100,null]}
}

What I want to get:

{
"name":"Some Name",
"details":{
          "subscriptions":["1"],
           "notes":["10 pcs"],
          "price":[100]
         }
}

Pls help, I am stuck.

Tried this in controller store method:

 $validated = $request->validated();
    $collection = collect($validated);

    $size = array_filter($validated['details']['size'], function (
      $value
    ) {
     return !is_null($value);
    });

This returns an empty array. But I am unable to figure out what to do next.

6
  • you need a cast laravel.com/docs/8.x/eloquent-mutators#array-and-json-casting Commented Oct 30, 2020 at 17:03
  • You want to allow null values to be sent, but filter then after validate? Commented Oct 30, 2020 at 17:04
  • @Svetoslav I have done that. Commented Oct 30, 2020 at 17:17
  • @FelippeDuarte Tried doing that using filter but wasn't successful. Can you pls help? Commented Oct 30, 2020 at 17:18
  • Validation isn't a filter. Where is the code where you try to filter the data you have? Commented Oct 30, 2020 at 17:19

2 Answers 2

1

You can filter whenever you have a null value, then filter again if the array is now empty. There are multiple ways to achieve this, this is only a possible solution.

<?php

$validated = json_decode('{
"name":"Some Name",
"details":{
          "subscriptions":["1"],
          "size":[null,null],
          "notes":["10 pcs",null],
          "price":[100,null]}
}', true);

//filter null values
foreach($validated['details'] as $key => $detail) {
    $validated['details'][$key] = array_filter($validated['details'][$key], function ($value) {
        return !is_null($value);
    });
}

//filter empty arrays
$validated['details'] = array_filter($validated['details'], function ($value) {
   return !empty($value);
});

print_r($validated);
Sign up to request clarification or add additional context in comments.

Comments

1

You can make a recursive filter.

    public function index(YourRequest $request)
    {
        $validated = $request->validated();

       $validated = $this->filterRecursive($validated);

       dd($validated);
    }

    public function filterRecursive($values)
    {
        foreach ($values as &$value) {
            if (is_array($value)) {
                $value = $this->filterRecursive($value);
            }
        }

        return array_filter($values);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.