efureev / laravel-support

PHP Support Package for Laravel

Stars
1

PHP Laravel Support


Codacy Badge
Build Status
PHP Laravel Package
Latest Stable Version
Total Downloads
Maintainability
Test Coverage

Install

composer require efureev/laravel-support "^0.3"

Usage

Traits\Database\UUID

Use UUID type for primary key

Schema::create(
    'table_name',
    static function (Blueprint $table) {
        static::columnUUID($table)->primary();
        $table->string('title');
        $table->timestamps();
    }
);

Use UUID type for foreign keys

Schema::create(
    'table_name',
    static function (Blueprint $table) {
        //...
        static::columnUUID($table, 'source_id', false)->nullable()->index();
        //...
    }
);

Types of $default:

  • string: NOW()
  • callable: needs return param of Illuminate\Database\Query\Expression class
  • class Illuminate\Database\Query\Expression

Traits\Models\PostgresArray

Scope for searching into PG arrays.
@see: \Php\Support\Laravel\Tests\Models\PgArrayModel::scopeByTag

Custom Casting

Use it for custom casting model's attributes. Even based on classes.
@see: \Php\Support\Laravel\Tests\Models\PgArrayModel::36
@see: \Php\Support\Laravel\Tests\Models\TestModel::33
@example:
Cast class:

use Php\Support\Laravel\Caster\AbstractCasting;

class Params extends AbstractCasting
{
    protected $key;
    
    public function toArray(): array
    {
        return [
            'key'       => $this->key,
        ];
    }
}

Model:

use Php\Support\Laravel\Caster\HasCasts;
class TestModel extends Model
{
    use HasCasts;

    protected $table = 'test_table';

    protected $fillable = ['params'];

    protected $casts = [
        'params'    => Params::class,
    ];
}

It's enough for use attribute params as class Params: $model->params->key!

Now, mutators are not needed. But they will work.

Test

composer test
composer test-cover # with coverage