2017 © Pedro Peláez
 

library metaphor

A Laravel package to manage meta values of a specific DB table.

image

sukohi/metaphor

A Laravel package to manage meta values of a specific DB table.

  • Sunday, October 11, 2015
  • by Sukohi
  • Repository
  • 2 Watchers
  • 1 Stars
  • 16 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 7 Versions
  • 0 % Grown

The README.md

Metaphor

A Laravel package that allows you to manage metadata. This package is maintained under Laravel 5.8., (*1)

Installation

Run the following command., (*2)

composer require sukohi/metaphor:3.*

Preparation

1. Trait

Set MetaphorTrait in your model as follows., (*3)

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Sukohi\Metaphor\MetaphorTrait;

class Item extends Model
{
    use MetaphorTrait;
}

2. Migration

Just run the migration command., (*4)

Note: You do NOT need to make any migrations by yourself because this package already has it., (*5)

php artisan migrate

That's it!, (*6)

Usage

Save

$item = \App\Item::find(1);
$item->meta->key_1 = 300;
$item->meta->key_2 = 'yyy';
$item->meta->key_3 = ['item_1x', 'item_2', 'item_3'];
$item->meta->key_4 = null;
$item->meta->save();

Note: $item->meta is an extended Collection of Laravel.
So you can use all of the methods as usual., (*7)

Delete

$item->meta->delete($key);

// or

$item->meta->deleteAll();

Check if a meta value exists

if($item->meta->has($key)) {

    // has it!

}

About appending

If you'd like metadata to include in model data, set meta to $appends., (*8)

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Sukohi\Metaphor\MetaphorTrait;

class Item extends Model
{
    use MetaphorTrait;
    protected $appends = ['meta'];  // <- here
}

Where clause

1. whereMeta

\App\Item::whereMeta('price', '500')->get();
\App\Item::whereMeta('price', 'LIKE', '%50%')->get();
\App\Item::orWhereMeta('price', '500')->get();
\App\Item::orWhereMeta('price', 'LIKE', '%50%')->get();

2. whereMetaIn

\App\Item::whereMetaIn('price', [300, 500])->get();
\App\Item::orWhereMetaIn('price', [300, 500])->get();

3. whereMetaNotIn

\App\Item::whereMetaNotIn('price', [300, 500])->get();
\App\Item::orWhereMetaNotIn('price', [300, 500])->get();

4. whereMetaNull

\App\Item::whereMetaNull('price')->get();
\App\Item::orWhereMetaNull('price')->get();

5. whereMetaNotNull

\App\Item::whereMetaNotNull('price')->get();
\App\Item::orWhereMetaNotNull('price')->get();

OrderByMeta

\App\Item::orderByMeta($key', 'asc')->get();
\App\Item::orderByMeta($key', 'desc')->get();

Note: This method uses FIELD(value, val1, val2, val3, ...) function in SQL.
It means if your DB system does not have the function, this feature is not available. MySQL has it, though., (*9)

License

This package is licensed under the MIT License.
Copyright 2019 Sukohi Kuhoh, (*10)

The Versions

11/10 2015

2.0.x-dev

2.0.9999999.9999999-dev

A Laravel package to manage meta values of a specific DB table.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

11/10 2015

dev-master

9999999-dev

A Laravel package to manage meta values of a specific DB table.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

11/10 2015

2.0.8

2.0.8.0

A Laravel package to manage meta values of a specific DB table.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

11/10 2015

2.0.7

2.0.7.0

A Laravel package to manage meta values of a specific DB table.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

11/10 2015

2.0.6

2.0.6.0

A Laravel package to manage meta values of a specific DB table.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

11/10 2015

2.0.5

2.0.5.0

A Laravel package to manage meta values of a specific DB table.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

10/10 2015

2.0.4

2.0.4.0

A Laravel package to manage meta values of a specific DB table.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi