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

sunscreem's avatar

Laravel changing data types?

Hi All,

(Noob disclaimer).

I've a weird problem. On my production machine, when I view the data types of my variables in my models, it seems they are getting changed from integer (as set in mysql) and converting them the strings. I'm finding I'm having to add mutators to the models to force them back to the correct type.

(Obviously) on my local dev environment, using an exact copy of the database, nothing is changing the types.

Have I possibly missed a setting somewhere?

0 likes
5 replies
sunscreem's avatar

Thanks Martin.

That's much cleaner than the mutators I've been adding.

I've been having a look around stack overflow and it seems that the sql drivers on windows and mac do pass the values through with the correct type, which explains the inconsistency with the production machine.

There is a fix for this with involves swapping the driver to a different one so I'll have fiddle with that as there are a lot of variables I'd have to cast on this project if not :(

Rob

billythekid's avatar
Level 8

Hi Rob,

I've just fixed this very same issue by upgrading the mysql driver php was using...

sudo apt-get install php5-mysqlnd

This means it uses PDO drivers rather than the old mysql/mysqli drivers. It removes the old ones and replaces them with it's own.

This appears to fix this issue without the need to use $casts. Though for belt and braces, it won't hurt to use $casts as well if you've already changed your models.

(don't forget to restart apache)

2 likes
raulledo's avatar

@billythekid I have my production server on DigitalOcean (installed via Laravel Forge) and I think that I have the "php5-mysqld" installed on that server.

To change that driver to "php5-mysqlnd" I only have to execute:

sudo apt-get install php5-mysqlnd

One the shell? I have to uninstall the other driver first or configure anything later?

Thanks!

Please or to participate in this conversation.