PHP code example of oza / laravel-database-jsonable
1. Go to this page and download the library: Download oza/laravel-database-jsonable library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
oza / laravel-database-jsonable example snippets
namespace App;
use Illuminate\Database\Eloquent\Model;
use Oza\DatabaseJsonable\Traits\DatabaseJsonable;
class Posts extends Model
{
Use DatabaseJsonable;
protected $jsonable = [
'actions'
];
protected $guarded = [];
}
$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
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
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 = [];
}
$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
$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
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 = [];
}
//e.g:
$post->actions->items->firstWhere('id', 1)->map(function ($value) {
return Str::camel($value);
})