spatie/laravel-schemaless-attributes
Composer 安装命令:
composer require spatie/laravel-schemaless-attributes
包简介
Add schemaless attributes to Eloquent models
README 文档
README
Wouldn't it be cool if you could have a bit of the spirit of NoSQL available in Eloquent? This package does just that. It provides a trait that when applied on a model, allows you to store arbitrary values in a single JSON column.
Here are a few examples. We're using the extra_attributes column here, but you can name it whatever you want.
// add and retrieve an attribute $yourModel->extra_attributes->name = 'value'; $yourModel->extra_attributes->name; // returns 'value' // you can also use the array approach $yourModel->extra_attributes['name'] = 'value'; $yourModel->extra_attributes['name'] // returns 'value' // setting multiple values in one go $yourModel->extra_attributes = [ 'rey' => ['side' => 'light'], 'snoke' => ['side' => 'dark'] ]; // setting/updating multiple values in one go via set() $yourModel->extra_attributes->set([ 'han' => ['side' => 'light'], 'snoke' => ['side' => 'dark'] ]); // retrieving values using dot notation $yourModel->extra_attributes->get('rey.side'); // returns 'light' // retrieve default value when attribute is not exists $yourModel->extra_attributes->get('non_existing', 'default'); // returns 'default' // it has a modelScope to retrieve all models with the given schemaless attributes $yourModel->withSchemalessAttributes(['name' => 'value', 'name2' => 'value2'])->get(); // delete key & value $yourModel->extra_attributes->forget('key');
Support us
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Requirements
This package requires a database with support for json columns like MySQL 5.7 or higher.
Installation
For Laravel versions 6 & 7 or PHP 7, use version 1.x of this package.
You can install the package via composer:
composer require spatie/laravel-schemaless-attributes
The schemaless attributes will be stored in a json column on the table of your model. Let's add that column and prepare the model.
Adding the column where the schemaless attributes will be stored
Add a migration for all models where you want to add schemaless attributes to. You should use schemalessAttributes method on Blueprint to add a column. The argument you give to schemalessAttributes is the column name that will be added. You can use any name you'd like. You're also free to add as many schemaless attribute columns to your table as you want. In all examples of this readme we'll use a single column named extra_attributes.
Schema::table('your_models', function (Blueprint $table) { $table->schemalessAttributes('extra_attributes'); });
Preparing the model
In order to work with the schemaless attributes you'll need to add a custom cast and a scope on your model. Here's an example of what you need to add if you've chosen extra_attributes as your column name.
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Builder; use Spatie\SchemalessAttributes\Casts\SchemalessAttributes; class TestModel extends Model { // ... public $casts = [ 'extra_attributes' => SchemalessAttributes::class, ]; public function scopeWithExtraAttributes(): Builder { return $this->extra_attributes->modelScope(); } // ... }
If you need support for multiple schemaless columns on a single model, you should use SchemalessAttributesTrait trait. Here's an example of what you need to add if you've chosen extra_attributes, other_extra_attributes as your column names.
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Builder; use Spatie\SchemalessAttributes\SchemalessAttributes; use Spatie\SchemalessAttributes\SchemalessAttributesTrait; class TestModel extends Model { use SchemalessAttributesTrait; // ... /** * @var array */ protected $schemalessAttributes = [ 'extra_attributes', 'other_extra_attributes', ]; public function scopeWithExtraAttributes(): Builder { return $this->extra_attributes->modelScope(); } public function scopeWithOtherExtraAttributes(): Builder { return $this->other_extra_attributes->modelScope(); } // ... }
If you want to reuse this behaviour across multiple models you could opt to put the function in a trait of your own. Here's what that trait could look like:
namespace App\Models\Concerns; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Builder; use Spatie\SchemalessAttributes\Casts\SchemalessAttributes; trait HasSchemalessAttributes { public function initializeHasSchemalessAttributes() { $this->casts['extra_attributes'] = SchemalessAttributes::class; } public function scopeWithExtraAttributes(): Builder { return $this->extra_attributes->modelScope(); } }
Usage
Getting and setting schemaless attributes
This is the easiest way to get and set schemaless attributes:
$yourModel->extra_attributes->name = 'value'; $yourModel->extra_attributes->name; // Returns 'value'
Alternatively you can use an array approach:
$yourModel->extra_attributes['name'] = 'value'; $yourModel->extra_attributes['name']; // Returns 'value'
You can replace all existing schemaless attributes by assigning an array to it.
// All existing schemaless attributes will be replaced $yourModel->extra_attributes = ['name' => 'value']; $yourModel->extra_attributes->all(); // Returns ['name' => 'value']
You can also opt to use get and set. The methods have support for dot notation.
$yourModel->extra_attributes = [ 'rey' => ['side' => 'light'], 'snoke' => ['side' => 'dark'], ]; $yourModel->extra_attributes->set('rey.side', 'dark'); $yourModel->extra_attributes->get('rey.side'); // Returns 'dark
You can also pass a default value to the get method.
$yourModel->extra_attributes->get('non_existing', 'default'); // Returns 'default'
Persisting schemaless attributes
To persist schemaless attributes you should, just like you do for normal attributes, call save() on the model.
$yourModel->save(); // Persists both normal and schemaless attributes
Retrieving models with specific schemaless attributes
Here's how you can use the provided modelScope.
// Returns all models that have all the given schemaless attributes $yourModel->withExtraAttributes(['name' => 'value', 'name2' => 'value2'])->get();
If you only want to search on a single custom attribute, you can use the modelScope like this
// returns all models that have a schemaless attribute `name` set to `value` $yourModel->withExtraAttributes('name', 'value')->get();
Also, if you only want to search on a single custom attribute with a custom operator, you can use the modelScope like this
// returns all models that have a schemaless attribute `name` starting with `value` $yourModel->withExtraAttributes('name', 'LIKE', 'value%')->get();
If you only want to search on a nested custom attribute, you can use the modelScope like this
// returns all models that have a schemaless nested attribute `han.side` set to `light` $yourModel->withExtraAttributes('han->side', 'light')->get();
Testing
First create a MySQL database named laravel_schemaless_attributes. After that you can run the tests with:
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.
spatie/laravel-schemaless-attributes 适用场景与选型建议
spatie/laravel-schemaless-attributes 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.87M 次下载、GitHub Stars 达 1.08k, 最近一次更新时间为 2018 年 05 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「spatie」 「laravel-schemaless-attributes」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 spatie/laravel-schemaless-attributes 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 spatie/laravel-schemaless-attributes 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 spatie/laravel-schemaless-attributes 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Receive webhooks in Laravel apps
Easily optimize images using PHP
Store your application settings
Add tags and taggable behaviour to your Laravel app
Speed up a Laravel application by caching the entire response additions
Manage events on a Google Calendar
统计信息
- 总下载量: 9.87M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1084
- 点击次数: 16
- 依赖项目数: 67
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-05-08