New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

inmanturbo/futomaki

Fat sushi rolls
3,921 1
Install
composer require inmanturbo/futomaki
Latest Version:v2.0.14
PHP:^8.2
Laravel:11
License:MIT
Last Updated:Dec 16, 2024
Links: GitHub  ·  Packagist
Maintainer: inmanturbo

Fat sushi rolls

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

A set of features for eloquent built on top of calebporzio/sushi.

Installation

You can install the package via composer:

composer require inmanturbo/futomaki

Usage

Eloquent CSV Driver

<?php

use Illuminate\Database\Eloquent\Model;
use Inmanturbo\Futomaki\HasCsv;

class Post extends Model
{
    use HasCsv;

    protected $schema = [
        'id' => 'id',
        'title' => 'string',
        'content' => 'text',
    ];

    protected function csvFileName()
    {
        return 'posts.csv';
    }

    protected function csvDirectory()
    {
        return storage_path('csv');
    }
}

HasCsv uses sushi (array driver) under the hood, and supports defining a $rows property as well. A csv file will automatically be created using the defined $rows.

use Illuminate\Database\Eloquent\Model;
use Inmanturbo\Futomaki\HasCsv;

class PostWithCsv extends Model
{
    use HasCsv;

    protected $guarded = [];

    protected $schema = [
        'id' => 'id',
        'title' => 'string',
        'content' => 'text',
    ];

    protected $rows = [
        [
            'id' => 1,
            'title' => 'Post 1',
            'content' => 'Content 1',
        ],
        [
            'id' => 2,
            'title' => 'Post 2',
            'content' => 'Content 2',
        ],
    ];
}

Using ->getCsvRows()

Implementing your own getCsvRows() method is supported as well.

use Illuminate\Database\Eloquent\Model;
use Inmanturbo\Futomaki\HasCsv;

class PostWithCsv extends Model
{
    use HasCsv;

    protected $guarded = [];

    protected $schema = [
        'id' => 'id',
        'title' => 'string',
        'content' => 'text',
    ];

    public function getCsvRows()
    {
       return [
            ['id' => 1,'title' => 'Post 1', 'content' => 'Content 1'],
            ['id' => 2,'title' => 'Post 2','content' => 'Content 2'],
        ];
    }
}

HasFutumakiWrites

HasFutomakiWrites is a trait which leverages eloquent's saving() and deleting() hooks to support writing sushi's changes out to another database, or api, etc. Example Below.


use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Inmanturbo\Futomaki\Futomaki;
use Inmanturbo\Futomaki\HasFutomakiWrites;

class PostWithFutomakiWrites extends Model
{
    use Futomaki;
    use HasFutomakiWrites;

    public $timestamps = true;

    public $guarded = [];

    protected $schema = [
        'id' => 'id',
        'title' => 'string',
        'content' => 'text',
    ];

    public function getRows()
    {
        return DB::connection('remote_posts')->table('posts')->get()->map(fn ($remoteItem) => [
            'id' => $remoteItem->id,
            'title' => $remoteItem->title,
            'content' => $remoteItem->body,
        ])->toArray();
    }

    public function futomakiSaving()
    {
        $values = [
            'id' => $this->id,
            'title' => $this->title,
            'body' => $this->content,
        ];

        DB::connection('remote_posts')->transaction(function () {
            DB::connection('remote_posts')->table('posts')->upsert($values, $this->getKeyName());
        });
    }

    public function futomakiDeleting()
    {
        DB::connection('remote_posts')
            ->table('posts')
            ->where($this->getKeyName(), $this->getKey())
            ->delete();
    }
}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

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

Credits

License

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

Related Packages

doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

648,266,899 9,701
laravel/framework

The Laravel Framework.

604,216,237 34,982
laravel/tinker

Powerful REPL for the Laravel framework.

512,508,631 7,443
laravel/serializable-closure

Laravel Serializable Closure provides an easy and secure way to serialize closur...

429,221,767 607
nunomaduro/collision

Cli error handling for console/command-line PHP applications.

407,699,310 4,663

Version History

Version Released PHP Laravel License
v2.0.14 ^8.2 ^11.0 MIT
v2.0.13 ^8.2 ^11.0 MIT
v2.0.12 ^8.2 ^11.0 MIT
v2.0.11 ^8.2 ^11.0 MIT
v2.0.8 ^8.2 ^11.0 MIT