Skip to content

Following Mass assignment rule with array cuase column not found error #65

Open
@Daniyal-Javani

Description

@Daniyal-Javani

This is the rquest body:

{
	"arr": [
		{
			"a": 3,
			"b": 3
		},
		{
			"a": 3,
			"b": 3
		}
	]
}

and with this validations rules:

    public function rules()
    {
        return [
            // 'arr' => 'array',
            'arr.*.a' => 'required'
        ];
    }

The result of dd($request->validated()); is what I want, I don't need b to be included

"arr" => array:2 [
    0 => array:1 [
      "a" => 3
    ]
    1 => array:1 [
      "a" => 3
    ]
  ]

But when I want to validate the array itself like this:

    public function rules()
    {
        return [
            'arr' => 'array',
            'arr.*.a' => 'required'
        ];
    }

b will be added to the output

"arr" => array:2 [
    0 => array:1 [
      "a" => 3,
      "b" => 3
    ]
    1 => array:1 [
      "a" => 3,
      "b" => 3
    ]
  ]

There is no way to exclue b from the array and it will cause the following error:

Message
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'base_require' in 'field list' (SQL: insert into product_variation_unit (base, base_require, max, min, name, product_variation_id, unit_id) values (1000, ?, 10000, 1000, کیلو, 2036, 1))
Level
ERROR
Exception
{
   "class": "Illuminate\\Database\\QueryException",
   "message": "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'base_require' in 'field list' (SQL: insert into product_variation_unit (base, base_require, max, min, name, product_variation_id, unit_id) values (1000, ?, 10000, 1000, \u06a9\u06cc\u0644\u0648, 2036, 1))",
   "code": "42S22",

Discussion about it: https://stackoverflow.com/questions/58851288/using-validated-method-with-array-in-laravel

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions