1. Go to this page and download the library: Download rhuett/csvie library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
rhuett / csvie example snippets
namespace App\Services\CsvCleaners;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Rhuett\Csvie\Cleaners\HashCsvCleaner;
/**
* Class ModelCleaner.
*
* An abstract CsvieCleaner implementation using a custom scrubbing technique based on your needs.
*/
class ModelCleaner extends HashCsvCleaner
{
/**
* Custom made function used to clean CSV data.
*
* @param array $rowData - The current row of data pulled from your CSV.
* @param ?\Illuminate\Support\Collection $foundModels - Matched model(s) based on your CSV, otherwise contains null.
* @param array $newModel - An empty model indexed with appropriate keys based on your model.
* @param \Illuminate\Support\Carbon $date - The current date used for timestamps.
* @param mixed $optionalData - Any custom data that you want to reference in the scrubber.
* @return array|null
*/
protected function scrubber(array $rowData, ?Collection $foundModels, array $newModel, Carbon $date, $optionalData)
{
// Run checks on $rowData here. Validate, cleanse or completely change!
// Use parent::updateValue() if you have many possible ways to update a single value within $rowData. Check the function for more information.
// Return any changes, otherwise return $rowData to make no changes.
// Return null if you want to remove the data completely.
// Note: Duplicated headers will be automatically renamed in $rowData.
// Ex: Header -> Header-1 -> Header-2 ...
// Note: Since we are not using Eloquent, we will need to manage our timestamps manually.
// This is why a Carbon datetime instance is passed as a parameter.
}
}
use Illuminate\Support\Collection;
use Rhuett\Csvie\Contracts\CsvieCleaner as CsvieCleanerContract;
use Rhuett\Csvie\Traits\CsvieHelpers;
/**
* Class MyCsvCleaner.
*
* An abstract CsvieCleaner implementation using a custom scrubbing technique based on your needs.
*/
abstract class MyCsvCleaner implements CsvieCleanerContract
{
use CsvieHelpers; // Not needed, review trait to see if it will help you.
/**
* Cleans the data within a CSV record to match what's expected by the database.
*
* @param \Illuminate\Support\Collection $data
* @return \Illuminate\Support\Collection
*/
public function scrub(Collection $data): Collection
{
// Clean all the data
}
}
php
public function store(Request $request)
{
$csvie = new Csvie; // note: you can pass an array of config overrides if needed
$modelInstance = new Model;
$referenceData = 'Whatever I want'; // note: reference data is optional
// Note: You can pass an array for both column and model UIDs if you need to verify against multiple columns
$cleaner = new ModelCleaner(
'ID', // column name from CSV file to match
'model_id', // model ID to verify against column name
$modelInstance,
$referenceData
);
$fileName = $request->file->store('/', 'uploads'); // move uploaded file from /temp into permanent storage
$chunkedFiles = $csvie->chunkFiles(
$csvie->getStorageDiskPath('uploads').$fileName
);
foreach($chunkedFiles as $chunk) {
$chunkData = $csvie->readCsvFile($chunk);
$cleanData = $cleaner->scrub($chunkData);
$cleanFile = $csvie->saveCsvFile($chunk, $cleanData);
$csvie->importCSV($cleanFile, $modelInstance);
}
$csvie->clearStorageDisk(); // clear out leftover uploaded file along with its chunks
return view('view.index')->with([
'models' => Model::all()
]);
}
bash
$ php artisan make:cleaner ModelNameCleaner
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.