oza / laravel-database-jsonable
Laravel 包,用于在 JSON 格式填充和检索数据库字段
1.0.2
2018-05-27 23:04 UTC
This package is auto-updated.
Last update: 2024-09-04 08:43:35 UTC
README
Laravel 包,用于在 JSON 格式填充和检索数据库字段
此包允许您以 JSON 格式填充模型的一些字段。例如,如果您有一个包含“操作”字段的帖子表,您可以在此字段中放置评论数、点赞数等操作,您可以使用此包轻松完成此操作
安装
composer require oza/laravel-database-jsonable
用法
- 只需将
DatabaseJsonable
特性和包含可序列化字段的$jsonable
属性添加到您的模型中。
<?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
- 现在您的
actions
字段将是一个包含许多方法的可序列化类,您可以使用这些方法在字段中添加、编辑、删除条目
$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
如果您这样做,模型的可序列化属性中包含的所有字段将直接编码为 JSON 并保存
始终键入数组以进行传输可能会有些累人。为了让您节省精力并继续编写优美的 Laravel 应用程序,可序列化字段可以遵循模式。要添加它,只需像这样修改您的可序列化属性
<?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 = []; }
然后您可以轻松添加如下数据
$post = Posts::first(); $id = $post->actions->add('like', 12345) //output 1
您还可以通过添加 strictJsonableSchema
属性来使用严格模式
<?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 = []; }
- 检索数据
所有保存的条目都是 Laravel 集合,这为您提供了许多方法,可以让您的生活更轻松。
$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
- 通过 ID 获取
通过 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
- 更改值或添加新条目
更改可序列化字段中条目的值
$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
- 更新条目
完全更改条目
$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
- 为条目添加时间戳
只需将 jsonableTimestamps
设置到您的模型中
<?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 = []; }
然后当您添加一些条目时,时间戳将被设置
- 每个条目都是 Laravel 集合
如上所述,所有条目都是 Laravel 集合,这为您打开了访问数组上许多方法的门户。有关所有可用方法,请参阅此处 Laravel 集合
//e.g: $post->actions->items->firstWhere('id', 1)->map(function ($value) { return Str::camel($value); })