Download the PHP package szwss/grade-tree without Composer

On this page you can find all versions of the php package szwss/grade-tree. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package grade-tree

grade-tree

grade tree model and observer for laravel 5

Install

 composer require szwss/grade-tree

Configure

在你的项目目录config/app.phpproviders数组里加入:

Szwss\GradeTree\GradeTreeProvider::class

使用说明

运行artisan

创建migration:

$ php artisan grade-tree:migration

默认的数据表名是grade_trees,如果需要指定数据表名,需要加上参数--table,例如:

$ php artisan grade-tree:migration --table=mytable

迁移数据表:

$ php artisan migrate

创建模型

使用artisan创建模型,例如:

$ php artisan make:model Cateory

然后,让你的模型use trait GradeTreeTrait

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    use \Szwss\GradeTree\GradeTreeTrait;
}

添加模型观察者

/app/Providers/EventServiceProvider.phpboot方法里添加:

GradeTree::observe(\Szwss\GradeTree\Observer::class);

Example

Create

$parent1 = new Category();
$parent1->name = 'Parent 1';
$parent1->save();

$parent2 = new Category();
$parent2->name = 'Parent 2';
$parent2->save();

$parent3 = new Category();
$parent3->name = 'Parent 3';
$parent3->save();

$parent4 = new Category();
$parent4->name = 'Parent 4';
$parent4->save();

$parent5 = new Category();
$parent5->name = 'Parent 5';
$parent5->save();

$child1 = new Category();
$child1->parent_id = $parent1->id;     //把parent_id字段设置为上级分类的id
$child1->name = 'Child 1';
$child1->save();

$child2 = new Category();
$child2->parent_id = $parent1->id;
$child2->name = 'Child 2';
$child2->save();

$child3 = new Category();
$child3->parent_id = $parent1->id;
$child3->name = 'Child 3';
$child3->save();

$grandchild1 = new Category();
$grandchild1->parent_id = $child1->id;
$grandchild1->name = 'Grandchild 1';
$grandchild1->save();

$grandchild2 = new Category();
$grandchild2->parent_id = $child1->id;
$grandchild2->name = 'Grandchild 2';
$grandchild2->save();

结果:

+----+--------+-------+--------------+----------+
| id | parent | level | name         | node     |
+----+--------+-------+--------------+----------+
|  1 |      0 |     1 | Parent 1     | ,1,      |
|  2 |      0 |     1 | Parent 2     | ,2,      |
|  3 |      0 |     1 | Parent 3     | ,3,      |
|  4 |      0 |     1 | Parent 4     | ,4,      |
|  5 |      0 |     1 | Parent 5     | ,5,      |
|  6 |      1 |     2 | Child 1      | ,1,6,    |
|  7 |      1 |     2 | Child 2      | ,1,7,    |
|  8 |      1 |     2 | Child 3      | ,1,8,    |
|  9 |      6 |     3 | Grandchild 1 | ,1,6,9,  |
| 10 |      6 |     3 | Grandchild 2 | ,1,6,10, |
+----+--------+-------+--------------+----------+

Update parent

$child1 = Category::find(6);
$child1->parent_id = 4;        //修改为id为4的子类
$child1->save();

结果:

+----+-----------+-------+--------------+----------+
| id | parent_id | level | name         | node     |
+----+-----------+-------+--------------+----------+
|  1 |         0 |     1 | Parent 1     | ,1,      |
|  2 |         0 |     1 | Parent 2     | ,2,      |
|  3 |         0 |     1 | Parent 3     | ,3,      |
|  4 |         0 |     1 | Parent 4     | ,4,      |
|  5 |         0 |     1 | Parent 5     | ,5,      |
|  6 |         4 |     2 | Child 1      | ,4,6,    |
|  7 |         1 |     2 | Child 2      | ,1,7,    |
|  8 |         1 |     2 | Child 3      | ,1,8,    |
|  9 |         6 |     4 | Grandchild 1 | ,4,6,9,  |
| 10 |         6 |     4 | Grandchild 2 | ,4,6,10, |
+----+-----------+-------+--------------+----------+

Delete

$parent4 = Category::find(4);
$parent4->delete();

结果:

+----+-----------+-------+----------+-------+
| id | parent_id | level | name     | node  |
+----+-----------+-------+----------+-------+
|  1 |         0 |     1 | Parent 1 | ,1,   |
|  2 |         0 |     1 | Parent 2 | ,2,   |
|  3 |         0 |     1 | Parent 3 | ,3,   |
|  5 |         0 |     1 | Parent 5 | ,5,   |
|  7 |         1 |     2 | Child 2  | ,1,7, |
|  8 |         1 |     2 | Child 3  | ,1,8, |
+----+-----------+-------+----------+-------+

Trait Methods

public \Illuminate\Database\Eloquent\Collection childrens(void)

获取所有子分类

$parent1 = Category::find(1);
dd($parent1->childrens());

public \Illuminate\Database\Eloquent\Collection getParent(void)

获取上级分类

$child1 = Category::find(6);
dd($child1->getParent());

You can use BelongTo Relation

$child1 = Category::find(6);
dd($child1->parent);

public \Illuminate\Database\Eloquent\Collection parents(void)

获取所有父分类


public static array getTree(callable $map = null)

public array tree(callable $map = null)

获取树数据结构

$map callable 处理原始数据的map方法,用于集合

$tree = Category::getTree(function($item) {
    $item->title = $item->name;
});


All versions of grade-tree with dependencies

PHP Build Version
Package Version
Requires php Version >=5.5.9
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package szwss/grade-tree contains the following files

Loading the files please wait ....