PHP code example of flatmodel / laravel-csv-flatmodel
1. Go to this page and download the library: Download flatmodel/laravel-csv-flatmodel 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/ */
flatmodel / laravel-csv-flatmodel example snippets
use App\Models\CsvModel
$model = (new CsvModel)
->where('active', true)
->pluck('id');
use FlatModel\CsvModel\Models\Model;
use FlatModel\CsvModel\Traits\Writable;
class EditableModel extends Model
{
use Writable;
protected string $path = 'data/users.csv';
protected bool $writable = true;
}
use FlatModel\CsvModel\Models\Model;
use FlatModel\CsvModel\Traits\Writable;
use FlatModel\CsvModel\Traits\Backupable;
class BackedUpModel extends Model
{
use Writable, Backupable;
protected string $path = 'data/users.csv';
protected bool $writable = true;
protected bool $enableBackup = true;
}
use FlatModel\CsvModel\Models\Model;
use FlatModel\CsvModel\Traits\Writable;
use FlatModel\CsvModel\Traits\AppendOnly;
class LogModel extends Model
{
use Writable, AppendOnly;
protected string $path = 'logs/activity.csv';
protected bool $writable = true;
protected bool $appendOnly = true;
}
use FlatModel\CsvModel\Models\Model;
use FlatModel\CsvModel\Traits\HeaderAware;
class StrictHeaderModel extends Model
{
use HeaderAware;
protected string $path = 'data/users.csv';
protected bool $hasHeaders = true; // CSV has header row
protected array $headers = ['id', 'name', 'email']; // Expected headers
protected bool $strictHeaders = true; // Enforce exact match
}
use FlatModel\CsvModel\Models\Model;
class Product extends Model
{
protected string $path = 'data/products.csv';
protected array $cast = [
'id' => 'int',
'price' => 'float',
'in_stock' => 'bool',
'name' => 'string',
];
}
// Values are automatically cast when reading
$product = (new Product())->where('id', 1)->first();
// $product['id'] is now an integer, not a string
// $product['price'] is now a float
// $product['in_stock'] is now a boolean
class DataModel extends Model
{
protected string $path = 'data/raw-data.csv';
protected bool $hasHeaders = false;
protected array $headers = ['id', 'name', 'value']; // Custom column names
}
// Access with your custom names
$model = new DataModel();
$model->where('name', 'John')->get();
class NumericModel extends Model
{
protected string $path = 'data/raw-data.csv';
protected bool $hasHeaders = false;
// No headers defined - will use: ['0', '1', '2', ...]
}
// Access with numeric indices
$model = new NumericModel();
$model->where('0', 'John')->get(); // First column
$model->pluck('2'); // Third column
$model = new CsvModel;
$model->insert(['id' => 5, 'name' => 'Alex']);
$model->flush(); // Writes changes to the file
$model = new CsvModel;
// Update a matching row
$model->update(
fn($row) => $row['id'] === 5,
fn($row) => [...$row, 'name' => 'Alexa']
);
// Upsert: update if found, insert if not
$model->upsert(
fn($row) => $row['id'] === 10,
fn($row) => ['id' => 10, 'name' => 'New User']
);
// Delete matching rows
$model->delete(fn($row) => $row['id'] === 2);
// Save changes to disk
$model->flush();