Search code examples
laravelmodelnosqlfillable

Can Laravel model validate the field data types(int,float,..etc) while inserting data?


My problem is that I'm using noSql for my database and Laravel for the backend. I know that we can validate the data types in the request but this validates only the incoming request after that we might calculate or re-arrange the data type of a field and insert it. I want to prevent this insertion by checking the data types. Is there a way to implement this validation in model like fillable [] or casts [].


Solution

  • You could setup an Observer on that model and in the Observer's 'updating' method you could perform any type formatting or cleanup required.

    Or if it's a real edge case you could call the Validator facade to define the keys/rules to check for and then pass your changed object to it? If Validator->fails() you could loop through Validator->errors() and apply corrections as needed.