medicivn / core
Installs: 7 444
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 1
Open Issues: 4
pkg:composer/medicivn/core
Requires
- php: ^8.1
- illuminate/database: ^9.17
- illuminate/http: ^9.17
- illuminate/pipeline: ^9.17
- illuminate/support: ^9.17
- intervention/image: ^2.7
Requires (Dev)
- laravel/legacy-factories: ^1.3
- nunomaduro/collision: ^6.2
- orchestra/testbench: ^7.5
- phpunit/phpunit: ^9.5
README
Medici core package
- Repository, Pipeline
- NodesTree
- EloquentNestedSet
- ApiResponser
- StatusCode, Error Handler
- Helpers
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 Image
- 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);
EloquentNestedSet
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';
Functions
getTree: get all nodes and return asnested arraygetFlatTree: get all nodes and return asflatten array, the child nodes will be sorted after the parent nodegetAncestors: get allancestornodes of current instancegetAncestorsTree: get allancestornodes of current instance and return asnested arraygetDescendants: get alldescendantnodes of current instancegetDescendantsTree: get alldescendantnodes of current instance and return asnested arrayparent: 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
Other
buildNestedTree: build a nested tree base onparent_id
Query scopes
The root node is automatically ignored by a global scope of ignore_root.
To get the root node, use withoutGlobalScope('ignore_root').
ancestorsdescendantsflattenTreeleafNodes
Warning
- If you are using
SoftDeleteand 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. SoftDeleteis required if you usequeue. Because thequeuewill not run indeletinganddeletedevents if a record is permanently deleted.