Lordbedwetter's avatar

Changing CSV data into Correct Datatypes

I am currently working on my admin to make my life easier. My current goal is to upload a vendor's csv file, and based on what the vendor id is, I will have different logic to upload different products. My main question however, is how I can manipulate the data that comes from my csv file and convert the fields into the correct datatypes in order to store them in my database. Here is my current code:


 derp

any advice is greatly appreciated.

Regards.

0 likes
3 replies
jlrdw's avatar

All I can say:

If company A's csv is always the same format, then you know the order of fields. What's numeric, what's string, etc. So I would probably write a converter per company to start with.

Later you may come up with a "master" converter.

You could have re-usable functions to handle field types, getters setters (accessors, mutators), that does the casting.

If it's going to be a large number of companies, I'd get with them and fully understand the formats used. The best of luck with this monstor.

1 like
Lordbedwetter's avatar

@JLRDW - Much appreciated. I have been using PHP and Laravel for some time now, but I still feel like a white belt at times. For now, I only have 3 vendors I will be doing this for, which is why I thought a switch statement would suffice for the time being, as I will be passing the vendor id along with the request. I was able to get a very simple test csv uploaded in a similar script, however, it did not encompass casting the data to the appropriate types, nor validating it either.

Do you know of any relatable material I could reference? I have been searching endlessly of other CSV related content, and while it is helping piece by piece, I am still unable to find what I am looking for.

Not too long ago, I found an article showing you how to cast the data into the preferred datatypes before saving to the database (in Laravel specifically). I can't for the life of me find it though.

Thanks again for the response and the well wishes. I need it badly lol

jlrdw's avatar

Just a thought, if this is "in house" have you thought about using Excel or another spreatsheet to retrieve the data, then after formatting in the spreadsheet use one of the php github utilities to finish getting into laravel / mysql.

Such as https://github.com/maatwebsite/Laravel-Excel

It might be a time saver in the long run.

1 like

Please or to participate in this conversation.