定制 adelaide/laravel-fluent 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

adelaide/laravel-fluent

最新稳定版本:v1.0.0

Composer 安装命令:

composer require adelaide/laravel-fluent

包简介

Fork of based/laravel-fluent. Define model attributes the fluent way. Automatically cast properties to native types.

README 文档

README

The package provides an expressive "fluent" way to define model attributes. It automatically builds casts at the runtime and adds a native autocompletion to the models' properties.

Introduction

With laravel-fluent, you can define Model attributes as you would do with any other class. The values will be transformed to the corresponding types depending on the native types of the properties.

Before:

<?php

/**
 * @property Collection $features
 * @property float $price
 * @property int $available
 */
class Product extends Model
{
    protected $casts = [
        'features' => 'collection',
        'price' => 'float',
        'available' => 'integer',
    ];
}

After:

<?php

class Product extends Model
{
    use HasFluentBindings;

    public Collection $features;
    public float $price;
    public int $available;
}

Installation

This version supports PHP 8.0. You can install the package via composer:

composer require based/laravel-fluent

Then, add the Based\Fluent\Fluent trait to your models:

<?php

class User extends Model
{
    use HasFluentBindings;
}

Model attributes

Define the public properties. laravel-fluent supports all native types and Laravel primitive casts:

<?php

class Order extends Model
{
    use HasFluentBindings;

    public int $amount;
    public Carbon $expires_at;

    #[AsDecimal(2)]
    public float $total;

    #[Cast('encrypted:array')]
    public array $payload;
}

Fillable and guarded

Any property being managed by the library can be marked as fillable or guarded by annotating it with an attribute.

<?php

class Order extends Model
{
    use HasFluentBindings;

    #[Guarded]
    public int $id;

    #[Fillable]
    public int $amount;

    #[Fillable]
    #[AsDecimal(2)]
    public float $total;

    #[Cast('encrypted:array')]
    public array $payload;
}

These attributes can also be specified at the class level, and they will affect multiple properties.

<?php

/*
 * All properties will be marked as fillable.
 * See Fillable's documentation for more nuanced controls.
 */
#[Fillable(Fillable::INCLUDE_ALL)]
class Order extends Model
{

    use HasFluentBindings;

    public int $id;
    public int $amount;

    #[AsDecimal(2)]
    public float $total;
}

Relations

The package also handles relationships.

<?php

class Product extends Model
{
    use HasFluentBindings;

    #[Relation]
    public Collection $features;
    public Category $category;

    public function features(): HasMany
    {
        return $this->hasMany(Feature::class);
    }

    public function category(): BelongsTo
    {
        return $this->belongsTo(Category::class);
    }
}

The package can automatically resolve relations from attributes, but be aware that there will be no autocompletion for the associated laravel-style methods.

<?php

class Product extends Model
{
    use HasFluentBindings;

    #[HasMany(Feature::class)]
    public Collection $features;
    #[BelongsTo]
    public Category $category;

    /*
     * The Laravel-style methods `features()` and `category()` will
     * be simulated under the hood, but will lack IDE autocompletion.
     */
}

Testing

composer test

Todo

  • Migration generator

Credits

License

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

统计信息

  • 总下载量: 33
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 26
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-11-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固