PHP code example of inmanturbo / futomaki

1. Go to this page and download the library: Download inmanturbo/futomaki 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/ */

    

inmanturbo / futomaki example snippets




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');
    }
}

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',
        ],
    ];
}

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'],
        ];
    }
}


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();
    }
}