Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Daniyal-Javani opened this issue Nov 14, 2019 · 0 comments

Comments

@Daniyal-Javani
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant