/laravel-flatfile

Eloquent flat file driver, on top of Sushi 🍣.

Primary LanguagePHPMIT LicenseMIT

authanram/laravel-flatfile

CI

Eloquent flat file driver, on top of Sushi 🍣.

Requirements

PHP 8.1.4 or higher, Laravel 9+

Downward compatibility is already at the doorstep.

Installation

You can install the package via composer.

composer require authanram/laravel-flatfile

By default all files written by this package will be located at storage_path('app/flatfile').

Publish the package configuration:

php artisan vendor:publish --provider="Authanram\FlatFile\FlatFileServiceProvider"

Quickly examining the configuration file config/flatfile.php would be a good idea.

Usage Example

Here's an example of how it can be used in a very basic way:

namespace App\Models;

use Authanram\FlatFile\FlatFileModel;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Post extends Model
{
    use FlatFileModel;
    use SoftDeletes;
    
    protected $schema = [
        'title' => 'string',
        'body' => 'string',
        'published_at' => 'datetime',
    ];
    
    protected $fillable = [
        'title',
        'body',
        'published_at',
    ];
}

Somewhere else in your code:

use App\Models\Post;

Post::create([
    'title' => 'New package arrived: laravel-flatfile',
    'body' => 'Solving the issue of...',
    'published_at' => now()->addHour(),
])

This will store the following contents to storage_path('app/flatfile/post/1.json'):

{
    "id": 1,
    "title": "New package arrived: laravel-flatfile",
    "body": "Solving the issue of...",
    "published_at": "2022-06-26 11:29:27",
    "created_at": "2022-06-26 10:29:27",
    "updated_at": "2022-06-26 10:29:27",
    "deleted_at": null
}

The package ships a second serializer, supporting yaml, that would lead to the following file contents stored at storage_path('app/flatfile/post/1.yaml'):

id: 1
title: New package arrived: laravel-flatfile
body: Solving the issue of...
published_at: 2022-06-26 11:29:27
created_at: '2022-06-26 10:29:27'
updated_at: '2022-06-26 10:29:27'
deleted_at: null

Caveats

  • many-to-many relationships are currently not supported

    In order to facilitate a many-to-many relationship, please fall back to a regular DBMS supported by Eloquent or feel free to create pull request.

Contributing

Please see the contribution guide for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

Special thanks to Caleb Porzio, the author of the underlying package Sushi 🍣.

License

The MIT License (MIT). Please see License File for more information.