在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):spatie/laravel-schemaless-attributes开源软件地址(OpenSource Url):https://github.com/spatie/laravel-schemaless-attributes开源编程语言(OpenSource Language):PHP 100.0%开源软件介绍(OpenSource Introduction):Add schemaless attributes to Eloquent modelsWouldn'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 // 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 usWe 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. RequirementsThis package requires a database with support for Installation
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 storedAdd a migration for all models where you want to add schemaless attributes to. You should use Schema::table('your_models', function (Blueprint $table) {
$table->schemalessAttributes('extra_attributes');
}); Preparing the modelIn 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 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 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();
}
} UsageGetting and setting schemaless attributesThis 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 $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 $yourModel->extra_attributes->get('non_existing', 'default'); // Returns 'default' Persisting schemaless attributesTo persist schemaless attributes you should, just like you do for normal attributes, call $yourModel->save(); // Persists both normal and schemaless attributes Retrieving models with specific schemaless attributesHere'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(); TestingFirst create a MySQL database named composer test ChangelogPlease see CHANGELOG for more information on what has changed recently. ContributingPlease see CONTRIBUTING for details. Security VulnerabilitiesPlease review our security policy on how to report security vulnerabilities. CreditsLicenseThe MIT License (MIT). Please see License File for more information. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论