0

I'm trying to validate a field using "in" and then passing an array befored defined. But when creating I get this error

"message": "Array to string conversion",

"exception": "ErrorException",

If I comment out the line where I use the validation, then works. So I'm pretty sure the problem is that.

I've seen another related posts, but they didn't work.

Thank you.

CONST ARRAY_EXAMPLES = [
    'example1'  => 0,
    'example2'  => 1,
    'example3'   => 2,
    'example4'  => 3,
    'example5' => 4,
    'example6'  => 5,
];

protected $fillable = [
    'array_example'
];
'array_example' => 'int|in:' .array_values(self::ARRAY_EXAMPLES)

1 Answer 1

2

When you concatenate two expressions in php both of them are casting to strings. But there is no any built in way to cast array to string. So you can't concatenate string and array here:

'int|in:' .array_values(self::ARRAY_EXAMPLES)

But you can convert array to string by implode:

'int|in:' . implode(',', self::ARRAY_EXAMPLES)
Sign up to request clarification or add additional context in comments.

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.