承接 oza/laravel-database-jsonable 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

oza/laravel-database-jsonable

Composer 安装命令:

composer require oza/laravel-database-jsonable

包简介

Laravel package to fill and retrieve your database fields in JSON formats

README 文档

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

namespace App;

use Illuminate\Database\Eloquent\Model;
use Oza\DatabaseJsonable\Traits\DatabaseJsonable;

class Posts extends Model
{
    Use DatabaseJsonable;

    protected $jsonable = [
        'actions'
    ];
    
    protected $guarded = [];
}

Api

  • Now your 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
  • You can also add data like this
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

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:

  $post = Posts::first();
  $id = $post->actions->add('like', 12345)
  //output 1

You can also use strict mode by adding strictJsonableSchema property

<?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, which gives you access to many methods that you can use to make your life easier.

 $post = Posts::first();
 $post->actions->all();
 // return an array of all items   
  • You can also retrieve data like this
$post->actions->items;
// Return a laravel Collection
  • Get First item
$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
$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

$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

$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

$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
 $post->actions->remove(2);
 // output true
  • Add timestamps to entries

just set jsonableTimestamps to your model

<?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

  • 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

//e.g:
$post->actions->items->firstWhere('id', 1)->map(function ($value) {
    return Str::camel($value);
})

oza/laravel-database-jsonable 适用场景与选型建议

oza/laravel-database-jsonable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 89 次下载、GitHub Stars 达 19, 最近一次更新时间为 2018 年 05 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 oza/laravel-database-jsonable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 oza/laravel-database-jsonable 我们能提供哪些服务?
定制开发 / 二次开发

基于 oza/laravel-database-jsonable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 89
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 19
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 19
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-05-13