Package Data | |
---|---|
Maintainer Username: | Sukohi |
Maintainer Contact: | capilano.sukohi@gmail.com (Sukohi) |
Package Create Date: | 2016-06-06 |
Package Last Update: | 2017-01-31 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-22 03:10:43 |
Package Statistics | |
---|---|
Total Downloads: | 30 |
Monthly Downloads: | 3 |
Daily Downloads: | 0 |
Total Stars: | 0 |
Total Watchers: | 2 |
Total Forks: | 0 |
Total Open Issues: | 0 |
A Laravel package to deal with array values that we can search through where clause.
This package is only for Laravel 5.3+.
Execute the following command.
composer require sukohi/eloquent-array:6.*
then set EloquentArrayServiceProvider in your config/app.php.
Sukohi\EloquentArray\EloquentArrayServiceProvider::class,
Execute the following command to publish and migrate the migration.
php artisan vendor:publish --provider="Sukohi\EloquentArray\EloquentArrayServiceProvider"
php artisan migrate
Then set EloquentArrayTrait
in your model like so.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Sukohi\EloquentArray\EloquentArrayTrait;
class Item extends Model
{
use EloquentArrayTrait;
}
$item = \App\Item::find(1);
$item->setArray('locales', [
'en' => 'English',
'es' => 'Spanish',
'ja' => 'Japanese'
]);
$item->saveArray();
$item->unsetArray('locales'); // Remove `locales`
$item->saveArray();
// Or
$item->unsetArray('locales', 'en'); // Remove `en`
$item->saveArray();
A specific array values related to an item will be removed.
\App\Item::find(1)->deleteArray('locales');
All of the array values related to an item will be removed.
\App\Item::find(1)->clearArray();
with Key
$item = \App\Item::find(1);
$array = $item->getArray('locales');
/*
Array
(
[en] => English
[es] => Spanish
[ja] => Japanese
)
*/
without Key
$array = $item->getArray('locales', false);
/*
Array
(
[0] => English
[1] => Spanish
[2] => Japanese
)
*/
All values
$array = $item->getAllArray();
A Specific Value
echo $item->getArrayValue('locales', 'en'); // English
// with Default Value
echo $item->getArrayValue('locales', 'en', 'Default-Value');
You can use whereArray() method to filter your data like so.
$items = \App\Item::whereArray('locales', 'en')->get();
// or
$items = \App\Item::where('id', 1)
->orWhereArray('locales', 'en')
->get();
You can use array items name for ORDER BY
like so.
$items = \App\Item::orderByArray('names', 'en')->get(); // asc
$items = \App\Item::orderByArray('names', 'en', 'asc')->get();
// or
$items = \App\Item::orderByArray('names', 'en', 'desc')->get();
[Set]
$item->setModelArray('App\User', 1); // Model ID
$item->saveArray();
[Set Array]
$item->setAllModelArray([
'App\User' => [1, 2, 3, 4, 5] // Model IDs
]);
$item->saveArray();
[Unset]
$item->unsetModelArray([
'App\User' => [2, 3] // Model IDs
]);
$item->saveArray();
[Clear]
$item->clearModelArray('App\User');
$item->saveArray();
// or
$item->clearModelArray([
'App\User',
'App\Item'
]);
$item->saveArray();
[Retrieve]
$users = $item->getModelArray('App\User');
This package is licensed under the MIT License.
Copyright 2016 Sukohi Kuhoh