Here is a small article that might be helpful: https://timacdonald.me/automatically-casting-eloquent-model-foreign-keys/
See also https://laravel.com/docs/5.6/eloquent-mutators#attribute-casting
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
In our DB monitoring, we typically see Eloquent casting string variables to nvarchar(4000) when building its queries. This can cause it to bypass an index on a varchar column since it has to do the implicit type cast.
Has anyone run into this before, and is there a way to default it to either not try to cast strings, or to define what they should be cast to, so that we can make use of our indexing?
Please or to participate in this conversation.