{"id":30699796,"url":"https://github.com/oza75/laravel-database-jsonable","last_synced_at":"2025-09-02T11:15:13.073Z","repository":{"id":57034693,"uuid":"133258232","full_name":"oza75/Laravel-Database-Jsonable","owner":"oza75","description":"Laravel package to fill and retrieve easily your database fields in json format","archived":false,"fork":false,"pushed_at":"2018-05-27T23:05:00.000Z","size":13,"stargazers_count":19,"open_issues_count":1,"forks_count":1,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-08-06T03:48:00.642Z","etag":null,"topics":["api","database","eloquent","json","laravel","orm"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/oza75.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-05-13T17:12:55.000Z","updated_at":"2022-01-03T10:45:38.000Z","dependencies_parsed_at":"2022-08-24T06:40:07.108Z","dependency_job_id":null,"html_url":"https://github.com/oza75/Laravel-Database-Jsonable","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/oza75/Laravel-Database-Jsonable","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oza75%2FLaravel-Database-Jsonable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oza75%2FLaravel-Database-Jsonable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oza75%2FLaravel-Database-Jsonable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oza75%2FLaravel-Database-Jsonable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/oza75","download_url":"https://codeload.github.com/oza75/Laravel-Database-Jsonable/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oza75%2FLaravel-Database-Jsonable/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273273573,"owners_count":25076194,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-02T02:00:09.530Z","response_time":77,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","database","eloquent","json","laravel","orm"],"created_at":"2025-09-02T11:15:07.447Z","updated_at":"2025-09-02T11:15:13.068Z","avatar_url":"https://github.com/oza75.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"### Laravel package to fill and retrieve your database fields in JSON formats\nthis package allows you to fill some fields of your model in json format.\nFor example, if you have a posts table, which has a field of action where\n you can put the actions as the number of comments likes etc ... \n you can easily do it with this package\n\n### Installation \n```\n   composer require oza/laravel-database-jsonable \n```\n### Usage\n  - Just add `DatabaseJsonable` trait and `$jsonable` \n property that contains jsonable fields to your model.\n \n ```php\n \u003c?php\n\n namespace App;\n \n use Illuminate\\Database\\Eloquent\\Model;\n use Oza\\DatabaseJsonable\\Traits\\DatabaseJsonable;\n \n class Posts extends Model\n {\n     Use DatabaseJsonable;\n \n     protected $jsonable = [\n         'actions'\n     ];\n     \n     protected $guarded = [];\n }\n ```\n ### Api\n \n - Now your `actions` field will be a Jsonable class that contains lots of methods that you can use to\n  add, edit, remove items in your field \n  ```php\n  $post = Posts::first();\n  $id = $post-\u003eactions-\u003eadd(['type' =\u003e 'like', 'count' =\u003e 1234])\n  //output 1\n  ```\n - You can also add data like this\n ```php\n Posts::create(['content' =\u003e 'blablab', 'actions' =\u003e ['type' =\u003e 'like', 'count' =\u003e 0] ]) \n // output an instance of App\\Posts\n ```\n  If you do this, all the fields contained in the jsonable property of your model will be directly encoded in json and saved\n - #### Define Schema for jsonable field\n Always typing an array to be transmitted can be a bit tiring.\n To allow you to save your energy and continue coding\n pretty Laravel application, the jsonable fields  can take a schema to follow.\n To add it,  just modify your jsonable property like that:\n ```php\n \u003c?php\n\n namespace App;\n \n use Illuminate\\Database\\Eloquent\\Model;\n use Oza\\DatabaseJsonable\\Traits\\DatabaseJsonable;\n \n class Posts extends Model\n {\n     Use DatabaseJsonable;\n \n     protected $jsonable = [\n         'actions' =\u003e [ 'type' , 'count' ]\n     ];\n     \n     protected $guarded = [];\n }\n ```\n Then you can easily add data like this:\n```php\n  $post = Posts::first();\n  $id = $post-\u003eactions-\u003eadd('like', 12345)\n  //output 1\n\n  ```\n  **You can also use strict mode by adding `strictJsonableSchema` property**   \n ```php\n \u003c?php\n\n namespace App;\n \n use Illuminate\\Database\\Eloquent\\Model;\n use Oza\\DatabaseJsonable\\Traits\\DatabaseJsonable;\n \n class Posts extends Model\n {\n     Use DatabaseJsonable;\n \n     protected $jsonable = [\n         'actions' =\u003e [ 'type' , 'count' ]\n     ];\n     \n     protected $strictJsonableSchema = true;\n     \n     protected $guarded = [];\n }\n ```   \n \n - Retrieve data\n \n All items saved are [Laravel Collection](https://laravel.com/docs/5.6/collections), which gives\n  you access to many methods that you can use to make your life easier.\n ```php\n  $post = Posts::first();\n  $post-\u003eactions-\u003eall();\n  // return an array of all items   \n``` \n- You can also retrieve data like this\n```php\n$post-\u003eactions-\u003eitems;\n// Return a laravel Collection\n```\n- Get First item \n```php\n$post-\u003eactions-\u003efirst(); \n// return a Laravel Collection\n\n$post-\u003eactions-\u003efirst()-\u003eall();\n// return an array\n\n$post-\u003eactions-\u003eitems-\u003efirst();\n// Return a Laravel Collection\n\n$post-\u003eactions-\u003eitems-\u003efirst()-\u003eall();\n// Return an array\n\n```\n- Get last Item\n```php\n$post-\u003eactions-\u003elast(); \n// return a Laravel Collection\n\n$post-\u003eactions-\u003elast()-\u003eall();\n// return an array\n\n$post-\u003eactions-\u003eitems-\u003elast();\n// Return a Laravel Collection\n\n$post-\u003eactions-\u003eitems-\u003elast()-\u003eall();\n// Return an array\n\n```\n- Get with id\n\nGet an item with its id\n```php\n$post = Posts::create(['contents' =\u003e 'blabla', 'actions' =\u003e ['type' =\u003e 'like', 'count' =\u003e 0]]);\n$id = $post-\u003eactions-\u003eadd(['like', 12345);\n$item = $post-\u003eactions-\u003eget($id); \n// return a Laravel Collection\n```\n- Change value or add new entry\n\nchange the value of an entry in your jsonable field\n```php\n$post = Posts::create(['contents' =\u003e 'blabla', 'actions' =\u003e ['type' =\u003e 'like', 'count' =\u003e 0]]);\n$id = $post-\u003eactions-\u003efirst()['id'];\n$post-\u003eactions-\u003eadd('like', 146);\n\n$item = $post-\u003eactions-\u003echange($id, 'count', 147); \n// return a Laravel Collection\n\n$item-\u003eget('count'); \n// output 147\n\n$item-\u003eget('count', 'default-value');\n// if a count key does not exist the default value will be return\n\n$item = $post-\u003eactions-\u003echange($id, 'user_id', 1);\n$item-\u003eget('user_id');\n//output 1\n```\n- Update an Item\n\nTotally change an entry\n```php\n$item = $post-\u003eactions-\u003eitems-\u003efirstWhere('id', 1);\n$item['count'] = 457;\n$item['type'] = 'comments';\n$item['user_id'] = 1\n$post-\u003eactions-\u003eupdate($item['id'], $item);\n// output \n[\n [\n    'count' =\u003e 457,\n    'type' =\u003e 'comments',\n    'user_id' =\u003e 1\n ]\n ...\n]\n\n```\n- Remove an item\n\n```php\n $post-\u003eactions-\u003eremove(2);\n // output true\n```\n\n- Add timestamps to entries\n\njust set `jsonableTimestamps` to your model\n```php\n\u003c?php\n\nnamespace App;\n\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Oza\\DatabaseJsonable\\Traits\\DatabaseJsonable;\n\nclass Posts extends Model\n{\n    Use DatabaseJsonable;\n\n    protected $jsonable = [\n        'actions' =\u003e [\n            'type', 'count'\n        ]\n    ];\n    \n     protected $strictJsonableSchema = true;\n     protected $jsonableTimestamps = true;\n     \n    protected $guarded = [];\n}\n\n```  \nThen when you add some items the timestamps will be set\n\n- Each item is a Laravel Collection\n\nAs I mentioned above all items are Laravel collections, \nwhich opens the door to many methods on array. \nFor all available methods, see here [Laravel Collection](https://laravel.com/docs/5.6/collections)\n\n```php\n//e.g:\n$post-\u003eactions-\u003eitems-\u003efirstWhere('id', 1)-\u003emap(function ($value) {\n    return Str::camel($value);\n})\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foza75%2Flaravel-database-jsonable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foza75%2Flaravel-database-jsonable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foza75%2Flaravel-database-jsonable/lists"}