定制 danielefavi/laravel-metadata 二次开发

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

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

danielefavi/laravel-metadata

Composer 安装命令:

composer require danielefavi/laravel-metadata

包简介

Laravel package for handling metadata for all your models.

README 文档

README

Laravel Metadata let you store extra data using your model without adding any extra field to your model.

$user->saveMeta('age', 25); // storing
$user->saveMeta('dog_name', 'Buddy'); // storing

$age = $user->getMeta('age');

Installation

Install the package via composer:

composer require danielefavi/laravel-metadata

Then add in the config/app.php file the entry DanieleFavi\Metadata\MetaServiceProvider::class, in the providers section:

    'providers' => [
        // ...
        DanieleFavi\Metadata\MetaServiceProvider::class,
    ],

Then run the migration:

php artisan migrate

How to use Laravel Metadata in your model

After installing the package you have to add the trait HasMetadata in your model.

For example

use DanieleFavi\Metadata\HasMetadata; // to add in your model

class User extends Authenticatable
{
    use HasFactory;
    use HasMetadata; // to add in your model
    
    // ...
}

Now your model has all the methods to handle the metadata.

Usage

Saving the metadata

Using the method saveMeta you can save (store or update) a metadata:

$user->saveMeta('phone_number', '111222333'); // storing a value
$user->saveMeta('color_preference', ['orange', 'yellow']); // storing an array

With saveMetas you can save multiple metadata at once:

$user->saveMetas([
    'phone_number' => '333222111',
    'color_preference' => ['red', 'green'],
    'address' => '29 Arlington Avenue',
]);

Getting the metadata

The method getMeta retrieve the metadata for the given key:

$phoneNumber = $user->getMeta('phone_number'); // the value of $phoneNumber is '111222333'

// Default value in case the metadata has not been found (default: null)
$anotherMeta = $user->getMeta('another_meta', 10);

You can retrieve the metadata in bulk

// return an array key => value with the metadata specified for the given keys
$metas = $user->getMetas(['phone_number', 'address']);
// the value of the $metas is an array key => value:
// [
//     'phone_number' => '111222333',
//     'address' => '29 Arlington Avenue'
// ]

// return an array key => value containing all metadata of the user model
$metas = $user->getMetas();

Getting the meta object by key

If you need to get the metadata object (and not just the value as getMeta and getMetas) you can use the method getMetaObj:

$metaObj = $user->getMetaObj('address');

Deleting the metadata

// delete a single metadata
$user->deleteMeta('address');

// delete multiple metadata
$user->deleteMeta(['phone_number', 'address']);

// delete all metadata
$user->deleteAllMeta();

Getting the meta objects of the model

Getting the collection of metadata attached to the model:

$list = $user->metas;

Querying

Getting all the users that have the hair color brown or pink.

$users = User::metaWhere('hair_color', 'brown')
            ->orMetaWhere('hair_color', 'pink')
            ->get();

Getting all the users with the hair color brown or pink and with a dog named Charlie:

$users = User::metaWhere('dog_name', 'charlie')
            ->where(function($query) {
                return $query->metaWhere('hair_color', 'brown')
                    ->orMetaWhere('hair_color', 'pink');
            })
            ->get();

Advanced Metadata Query

You can query the metadata using has, whereHas and with. For example:

$users = User::whereHas('metas', function($query) {
    $query->where('key', 'hair_color');
    $query->where('value', json_encode('blue')); // remember to json_encode the value!!!
})->get();

Important: when doing your custom queries remember to JSON encode the metavalue because in the DB the metavalue is stored as JSON.
In the example above

Eager Loading the Metadata

Getting all the users with all their metadata:

$users = User::with('metas')->get();

Or lazy loading the metadata:

$user = User::find(1);

$user->load('metas');

统计信息

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

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-06-27

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固