Import CSV with array column and urls with commas

I was wondering if there is a way to pre-check the CSV similar to how Shopify does is with user imports. I have a user that has as CSV column that contains a list of urls, separated with a comma, which works perfectly fine, unless the urls contain commas.

The column in questions can look something like this in the raw CSV:

"https://www.example.ch/media/2gqo4z3g/1189766387_ret.jpg?rxy=0.4613560813453266,0.38044619707701444&width=1230&height=820&format=webp&quality=80,https://www.example.ch/media/2gqo4z3g/1189766111_ret.jpg?rxy=0.4613560813453266,0.38044619707701444&width=1230&height=820&format=webp&quality=80"

Ideas on how to handle this:
  • Custom validation rule for the ImportColumn
  • Cast the state differently
Solution
The solution was to use castStateUsing on the ImportColumn:

ImportColumn::make('extra_images')
  ->rules(['nullable', 'array', 'max:20'])
  ->castStateUsing(function (?string $state): array {
      if (blank($state)) {
          return [];
      }

      preg_match_all('/https?:\/\/[^\s,]+/', $state, $matches);

      $filtered = array_filter($matches[0] ?? [], function ($url) {
          return filter_var($url, FILTER_VALIDATE_URL);
      });

      return array_unique($filtered);
  }),
Was this page helpful?