Search code examples
phplaravellaravel-formrequest

form request nestead array in laravel


I am using Laravel and I have a sorting system and I want to use form request to validate it

so the request is this

    array:5 [▼
  "type" => "product"
  "minprice" => "10"
  "maxprice" => "10000000000"
  "color" => null
  "sortBy" => array:2 [▼
    "field" => "created_at"
    "orderBy" => "desc"
  ]
]

and I want to check 'field' and 'orderBy' to only have for example created_at value and desc value

How can I do this?

here is my form reqeust

  return [
      'type' => 'required|in:all,file,service,product',
      'minprice' =>  'required|numeric|min:0|max:1000000000000',
      'maxprice' =>  'required|numeric|min:0|max:100000000000000',
      'color' =>  'min:0|max:7',
      // 'sortBy' =>  'in:desc,orderBy',
    ];

Solution

  • Read the docs at: https://laravel.com/docs/6.x/validation

    It says:

    If your HTTP request contains "nested" parameters, you may specify them in your validation rules using "dot" syntax:

    So you can use:

    'sortBy.field' => 'in:created_at,updated_at,deleted_at',
    'sortBy.orderBy' => 'in:asc,desc',