bfinlay / laravel-excel-seeder

Seed your database with Laravel using Excel and CSV files
Other
38 stars 8 forks source link

Parser Settings #15

Closed humayunjavaid closed 1 year ago

humayunjavaid commented 1 year ago

Hi How can we parse column values?

i am using $this->parsers = [ 'account_number' => function ($value) { return 'test'.$value; } ];. But it is not parsing.

bfinlay commented 1 year ago

It does not currently. This is a feature added to laravel-csv-seeder Sep 2020 and this package forked off in June 2019. It's a feature I've had in mind to add since the fork (even before laravel-csv-seeder added it) but we haven't needed to use it yet so it hasn't made it in.

I'll take a look at it and add it in.

humayunjavaid commented 1 year ago

Infact i am using $model::creating event in boot method but it does not work with seeder but works with default seeder class. any thoughts?

Thanks

bfinlay commented 1 year ago

I have released v3.2.0 which adds the parsers.

You shouldn't need the $model::creating event now for parsers.

The way that this package is designed, it doesn't use models to write rows to the database so the $model::creating would not be invoked, currently.

bfinlay commented 1 year ago

Closing issue as completed but feel free to add comments if you see more issues, or to confirm that it works for you.

bfinlay commented 1 year ago

$this->parsers = [ 'account_number' => function ($value) { return 'test'.$value; } ];

This parser function as written would not work. [update: this is actually fine. see thread.] I'm not sure if 'test' is meant to refer to something. The $value passed to the function will be the $value for a row in the 'account_number' column. The closure can transform this value, for example, if account_number were alphanumeric and you wanted it to be converted to lowercase:

$this->parsers = [ 'account_number' => function ($value) { return strtolower($value); } ];
humayunjavaid commented 1 year ago

In this parser i am just appending random value to test for a row in account_number column.

Basically i want to append same random code for each row value in the column account number.

For example i have account_number = 5 and through parser i need to prepend 220000 code. and it will store 2200005 in database.

bfinlay commented 1 year ago

I see! I made the classic PHP error of mistaking the concatenation operator for the field access operator, sorry.

The parser as you wrote it would work fine for that!