https://github.com/oza75/laravel-database-jsonable
Laravel package to fill and retrieve easily your database fields in json format
https://github.com/oza75/laravel-database-jsonable
api database eloquent json laravel orm
Last synced: 10 months ago
JSON representation
Laravel package to fill and retrieve easily your database fields in json format
- Host: GitHub
- URL: https://github.com/oza75/laravel-database-jsonable
- Owner: oza75
- Created: 2018-05-13T17:12:55.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-05-27T23:05:00.000Z (about 8 years ago)
- Last Synced: 2025-08-06T03:48:00.642Z (11 months ago)
- Topics: api, database, eloquent, json, laravel, orm
- Language: PHP
- Size: 12.7 KB
- Stars: 19
- Watchers: 0
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
### Laravel package to fill and retrieve your database fields in JSON formats
this package allows you to fill some fields of your model in json format.
For example, if you have a posts table, which has a field of action where
you can put the actions as the number of comments likes etc ...
you can easily do it with this package
### Installation
```
composer require oza/laravel-database-jsonable
```
### Usage
- Just add `DatabaseJsonable` trait and `$jsonable`
property that contains jsonable fields to your model.
```php
actions->add(['type' => 'like', 'count' => 1234])
//output 1
```
- You can also add data like this
```php
Posts::create(['content' => 'blablab', 'actions' => ['type' => 'like', 'count' => 0] ])
// output an instance of App\Posts
```
If you do this, all the fields contained in the jsonable property of your model will be directly encoded in json and saved
- #### Define Schema for jsonable field
Always typing an array to be transmitted can be a bit tiring.
To allow you to save your energy and continue coding
pretty Laravel application, the jsonable fields can take a schema to follow.
To add it, just modify your jsonable property like that:
```php
[ 'type' , 'count' ]
];
protected $guarded = [];
}
```
Then you can easily add data like this:
```php
$post = Posts::first();
$id = $post->actions->add('like', 12345)
//output 1
```
**You can also use strict mode by adding `strictJsonableSchema` property**
```php
[ 'type' , 'count' ]
];
protected $strictJsonableSchema = true;
protected $guarded = [];
}
```
- Retrieve data
All items saved are [Laravel Collection](https://laravel.com/docs/5.6/collections), which gives
you access to many methods that you can use to make your life easier.
```php
$post = Posts::first();
$post->actions->all();
// return an array of all items
```
- You can also retrieve data like this
```php
$post->actions->items;
// Return a laravel Collection
```
- Get First item
```php
$post->actions->first();
// return a Laravel Collection
$post->actions->first()->all();
// return an array
$post->actions->items->first();
// Return a Laravel Collection
$post->actions->items->first()->all();
// Return an array
```
- Get last Item
```php
$post->actions->last();
// return a Laravel Collection
$post->actions->last()->all();
// return an array
$post->actions->items->last();
// Return a Laravel Collection
$post->actions->items->last()->all();
// Return an array
```
- Get with id
Get an item with its id
```php
$post = Posts::create(['contents' => 'blabla', 'actions' => ['type' => 'like', 'count' => 0]]);
$id = $post->actions->add(['like', 12345);
$item = $post->actions->get($id);
// return a Laravel Collection
```
- Change value or add new entry
change the value of an entry in your jsonable field
```php
$post = Posts::create(['contents' => 'blabla', 'actions' => ['type' => 'like', 'count' => 0]]);
$id = $post->actions->first()['id'];
$post->actions->add('like', 146);
$item = $post->actions->change($id, 'count', 147);
// return a Laravel Collection
$item->get('count');
// output 147
$item->get('count', 'default-value');
// if a count key does not exist the default value will be return
$item = $post->actions->change($id, 'user_id', 1);
$item->get('user_id');
//output 1
```
- Update an Item
Totally change an entry
```php
$item = $post->actions->items->firstWhere('id', 1);
$item['count'] = 457;
$item['type'] = 'comments';
$item['user_id'] = 1
$post->actions->update($item['id'], $item);
// output
[
[
'count' => 457,
'type' => 'comments',
'user_id' => 1
]
...
]
```
- Remove an item
```php
$post->actions->remove(2);
// output true
```
- Add timestamps to entries
just set `jsonableTimestamps` to your model
```php
[
'type', 'count'
]
];
protected $strictJsonableSchema = true;
protected $jsonableTimestamps = true;
protected $guarded = [];
}
```
Then when you add some items the timestamps will be set
- Each item is a Laravel Collection
As I mentioned above all items are Laravel collections,
which opens the door to many methods on array.
For all available methods, see here [Laravel Collection](https://laravel.com/docs/5.6/collections)
```php
//e.g:
$post->actions->items->firstWhere('id', 1)->map(function ($value) {
return Str::camel($value);
})
```