composer require efureev/laravel-support "^1.17"
Read /docs
for more documentations.
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
Scope for searching into PG arrays. @see: \Php\Support\Laravel\Tests\Models\PgArrayModel::scopeByTag
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.
composer test
composer test-cover # with coverage