Medici core package
- Repository, Pipeline
- NodesTree
- EloquentNestedSet
- ApiResponser
- StatusCode, Error Handler
- Helpers
Add MediciCoreHelperServiceProvider
to config/app.php
, example:
'providers' => [
...
MediciVN\Core\Providers\MediciCoreHelperServiceProvider::class,
],
Functions:
- upload_images
- resize_image
- upload_private_images
- get_url_private
- medici_logger
- generate_random_verification_code
- upload_image_v2
- upload_private_image_v2
- Upload an image
$disk = Storage::disk(env('FILESYSTEM_CLOUD_PRIVATE', 's3')); // disk driver instance
$uploader = new Uploader($source, $disk, $path); // uploader instance
$uploader->setSizes($size); // resize image if required
$uploader->setFileName($fileName); // want to set a specific file name
$uploader->upload()->getResult(); // upload and get result
// or you can chain the methods in one line
$uploader->setSizes($size)->setFileName($fileName)->upload()->getResult();
// global function
upload_image_v2($source, $path, $size, $fileName);
Automatically update the tree when creating, updating and deleting a node.
How to use:
- First, a root node must be initialized in your model's table
- Add
use EloquentNestedSet;
to your eloquent model, example:
class Category extends Model
{
use EloquentNestedSet;
/**
* The root node id
* Default: 1
*/
const ROOT_ID = 99999;
/**
* The left position column name
* Default: 'lft'
*
* Note: 'lft' can be a negative number
*/
const LEFT = 'lft';
/**
* The right position column name
* Default: 'rgt'
*
* Note: 'rgt' can be a negative number
*/
const RIGHT = 'rgt';
/**
* The parent's id column name
* Default: 'parent_id'
*/
const PARENT_ID = 'parent_id';
/**
* The depth column name
* The depth of a node - nth descendant, it doesn't affect left and right calculation
* Starting from the root node will have a depth of 0
* Default: 'depth'
*/
const DEPTH = 'depth';
/**
* The queue connection is declared in your project `config/queue.php`.
* if QUEUE_CONNECTION and QUEUE are not provided, lft and rgt calculation are synchronized.
*
* Default: null
*/
const QUEUE_CONNECTION = 'sqs';
/**
* Default: null
*/
const QUEUE = 'your_queue';
getTree
: get all nodes and return asnested array
getFlatTree
: get all nodes and return asflatten array
, the child nodes will be sorted after the parent nodegetAncestors
: get allancestor
nodes of current instancegetAncestorsTree
: get allancestor
nodes of current instance and return asnested array
getDescendants
: get alldescendant
nodes of current instancegetDescendantsTree
: get alldescendant
nodes of current instance and return asnested array
parent
: get the parent node to which the current instance belongschildren
: get the child nodes of the current instancegetLeafNodes
: get all leaf nodes - nodes with no children
buildNestedTree
: build a nested tree base onparent_id
The root
node is automatically ignored by a global scope of ignore_root
.
To get the root
node, use withoutGlobalScope('ignore_root')
.
ancestors
descendants
flattenTree
leafNodes
- If you are using
SoftDelete
and intend to stop using it, you must deal with soft deleted records. The tree will be shuffled, and the calculation of lft and rgt may go wrong. SoftDelete
is required if you usequeue
. Because thequeue
will not run indeleting
anddeleted
events if a record is permanently deleted.