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, (*1)
composer require oza/laravel-database-jsonable
Just add DatabaseJsonable
trait and $jsonable
property that contains jsonable fields to your model., (*2)
<?php namespace App; use Illuminate\Database\Eloquent\Model; use Oza\DatabaseJsonable\Traits\DatabaseJsonable; class Posts extends Model { Use DatabaseJsonable; protected $jsonable = [ 'actions' ]; protected $guarded = []; }
actions
field will be a Jsonable class that contains lots of methods that you can use to
add, edit, remove items in your field $post = Posts::first(); $id = $post->actions->add(['type' => 'like', 'count' => 1234]) //output 1
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, (*3)
<?php namespace App; use Illuminate\Database\Eloquent\Model; use Oza\DatabaseJsonable\Traits\DatabaseJsonable; class Posts extends Model { Use DatabaseJsonable; protected $jsonable = [ 'actions' => [ 'type' , 'count' ] ]; protected $guarded = []; }
Then you can easily add data like this:, (*4)
$post = Posts::first(); $id = $post->actions->add('like', 12345) //output 1 ``` **You can also use strict mode by adding `strictJsonableSchema` property** ```php <?php namespace App; use Illuminate\Database\Eloquent\Model; use Oza\DatabaseJsonable\Traits\DatabaseJsonable; class Posts extends Model { Use DatabaseJsonable; protected $jsonable = [ 'actions' => [ '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
$post->actions->items; // Return a laravel Collection
$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
$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 an item with its id, (*5)
$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 the value of an entry in your jsonable field, (*6)
$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
Totally change an entry, (*7)
$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 ] ... ]
$post->actions->remove(2); // output true
just set jsonableTimestamps
to your model, (*8)
<?php namespace App; use Illuminate\Database\Eloquent\Model; use Oza\DatabaseJsonable\Traits\DatabaseJsonable; class Posts extends Model { Use DatabaseJsonable; protected $jsonable = [ 'actions' => [ 'type', 'count' ] ]; protected $strictJsonableSchema = true; protected $jsonableTimestamps = true; protected $guarded = []; }
Then when you add some items the timestamps will be set, (*9)
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, (*10)
//e.g: $post->actions->items->firstWhere('id', 1)->map(function ($value) { return Str::camel($value); })