Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jove's avatar
Level 7

Request data is supposed to be integers but show up as strings (array)

So I'm passing an array with ID's from a simple multiselect. Now when I access $request->myarray the ID's are there, but they are strings, even if I try validate it as numeric / integer.

This does not work for me, I need the integers so I can save it as an array to my DB and do a lookup later on.

Why does my integers get turned to strings?

0 likes
10 replies
lostdreamer_nl's avatar

POST requests can only POST strings, it's just how the HTTP protocol works.

If you really need to be posting integers or anything but strings, you should be posting JSON (which actually has types).

Normally, you'd simply cast the data into the correct type before validating.

If you want to cast them and you are using the FormRequest validation methods of laravel you can override the following method in your request class to do so:

    protected function validationData()
    {
        $data = $this->all();
        foreach($data['my_array'] as $key => $num) {
            $data['my_array'][$key] = (int) $num;
        }
        return $data;
    }

Besides that, if your database has the field set as int and you try to insert the string '2' it will convert it to the integer 2 while saving, so it shouldn't matter in the case you describe.

1 like
jove's avatar
Level 7

I don't want to override anything on a global scale, only for this controller / model

jove's avatar
Level 7

I guess I can do this

if ($request->has('myarray'))
{
  $temp = collect();
  foreach ($request->myarray as $data)
  {
    $temp->push((int)$data);
  }
  $request->replace(['myarray' => $temp->toArray()]);
}

ofc, with checks to make sure it is an array and that it only contains integers Any better way?

lostdreamer_nl's avatar

what I ment with overriding was for this request only ;)

https://laravel.com/docs/5.7/validation#form-request-validation

It would make your controller endpoint look like:

public function store(PostRequest $request) 
{
    $post = Post::create( $request->all() );
    return $post;
}

So only the controllers that are using the PostRequest instead of the normal Request class will have this override in there.

It's the best way to do your validation anyway, instead of putting all that code into the controller or model.

jove's avatar
Level 7

@lostdreamer_nl I tried, did not work, So the request override does in fact now return a array of integers, tho the request in the controller does not

munazzil's avatar

@joveice Check below Code.Show the result.

public function store(Request $request) 
{   
  $data= $request->all();
     dd($data);
  Post::create($data);

}
jove's avatar
Level 7

@munazzil unchanged.

But if I return

 $data = collect();
 foreach($this->myarray as $num) {
     $data->push((int) $num);
 }
 return $this->merge(['myarray' => $data]);

it works.

This how ever

$data = $this->all();
 foreach($data['my_array'] as $key => $num) {
     $data['my_array'][$key] = (int) $num;
 }
 return $data;

Does not, tho if I dump $data before return it shows the changed values, but the $request does not reflect it.

lostdreamer_nl's avatar
Level 53

Check my first post again, but instead of overriding the validationData() method, override the all() method in the same way.


    public function all($keys = null)
    {
        $data = parent::all($keys);
        if(isset($data['my_array'])) {
            $data['my_array'] = array_map('intval', $data['my_array']);
        }
        return $data;
    }

And use it as:

public function store(PostRequest $request) 
{
    // $request->all() will now transform the keys to integers
    $post = Post::create( $request->all() );
    return $post;
}

But again, if your DB has the field set as an integer, it shouldn't matter if you are inserting strings, as long as they are number strings the DB should convert them for you, this all is not necessary.

1 like
kt200707g11's avatar

use this package with parameter cast in your validation rules

composer require qtlenh/laravel-strict-validator

example rule:

['ids' => 'array', 'ids.*' => 'integer:cast']

Please or to participate in this conversation.