Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Laravel Nested Sets v7 - Laravel 11, PHP 8.3, PHPUnit 10.* and drop EOL PHP support #599

Open
wants to merge 71 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
927fd8f
Fixes the join problem
SlyDeath May 16, 2017
b0a05e0
Update QueryBuilder.php
SlyDeath May 22, 2017
85131b2
Update QueryBuilder.php
SlyDeath Jul 4, 2017
c739a2d
Minimum stability
lazychaser Aug 30, 2017
0adbe74
Bump minor version
lazychaser Aug 30, 2017
892b9a7
Fixed #252: nested eagers for ancestors and descendants are now loaded
lazychaser Nov 26, 2017
9092b43
Remove hardcoded refs to "id" as PK
darkspotinthecorner Jan 31, 2018
c4a05f9
Merge pull request #264 from darkspotinthecorner/patch-1
lazychaser Feb 1, 2018
6151674
Bump version
lazychaser Feb 4, 2018
65b08fb
Add blueprint macros
lazychaser Feb 4, 2018
c2682f6
Merge branch 'v4' of github.com:lazychaser/laravel-nestedset into v4
lazychaser Feb 4, 2018
179962b
Update
lazychaser Feb 4, 2018
3265da5
Update
lazychaser Feb 4, 2018
b570bc6
Merge pull request #218 from SlyDeath/SlyDeath-patch-1
lazychaser Mar 27, 2018
e8b6883
Update README.markdown
lazychaser May 8, 2018
82c543b
Support Laravel 5.7
bonzai Sep 4, 2018
8f2882b
Merge pull request #307 from bonzai/v4
lazychaser Sep 4, 2018
dd9434d
Fix "ambiguous column" issue when using whereAncestorOr or whereDesce…
Sep 25, 2018
91601c3
Merge pull request #315 from kelyak/kelyak-patch-1
lazychaser Sep 30, 2018
0865b9c
Support Laravel 5.8
billriess Feb 26, 2019
d4945f6
Support Laravel 5.8
billriess Feb 26, 2019
1b3f916
Support Laravel 5.8
billriess Feb 26, 2019
351fab2
Merge pull request #354 from billriess/v4
lazychaser Feb 27, 2019
bed43e0
Require Laravel 5.7 and PHP 7.1
Ciaro Apr 12, 2019
1f9bb95
Fix eager loading issue ancestors/descendants relationships (fixes #3…
Ciaro Apr 12, 2019
10732f1
Split off v5 branch to indicate BC for Laravel 5.7 and 5.8
Ciaro Apr 21, 2019
086807a
Merge pull request #370 from Ciaro/v5
lazychaser Jun 16, 2019
9c0ae24
SUpport l6.0
lazychaser Sep 6, 2019
f6f6d73
Fixed readme
lazychaser Sep 6, 2019
31832f7
Use caret in L6 version name
antonkomarev Sep 6, 2019
48f08a7
Merge pull request #402 from cybercog/fix/laravel-6-support
lazychaser Sep 6, 2019
173280d
Bump laravel ver
lazychaser Mar 4, 2020
d4fe17f
Merge branch 'v5' of github.com:lazychaser/laravel-nestedset into v5
lazychaser Mar 4, 2020
b394b05
Add support for Laravel 8
agentphoenix Sep 8, 2020
a4c9728
Add PHP 7.4 to test suite
agentphoenix Sep 9, 2020
ea701ed
Revert PHP version requirement
agentphoenix Sep 9, 2020
1edd06d
Merge pull request #472 from agentphoenix/v5
lazychaser Sep 11, 2020
b6233fd
update getRelationCountHash
yurykozyrev Dec 1, 2020
b2c98cc
add phpdoc
yurykozyrev Dec 1, 2020
789a70b
Merge pull request #478 from urakozz/v5
lazychaser Dec 7, 2020
cd0fecb
Lock laravel version
lazychaser May 28, 2021
f535123
Fix incompatibility
lazychaser May 28, 2021
abf4c86
Update
lazychaser May 28, 2021
46e8c27
Added version v6
vokamut May 28, 2021
41a445d
Adding Github actions, removing Travis CI
emielmolenaar Jun 15, 2021
427d428
fix issues with novas reverse relation resolver
voydz Nov 8, 2021
4efffc8
Init Laravel 9 support
ifox Jan 25, 2022
9c307bf
Add PHP 8.0 to tests
ifox Jan 25, 2022
23f35c7
Merge branch 'pr/1' into v6
ifox Feb 9, 2022
cf3e49a
Remove PHP 7.1 from tests matrix
ifox Feb 9, 2022
68519a1
Support PHPUnit updates
ifox Feb 9, 2022
47bd676
Add PHP 8.1 to tests matrix
ifox Feb 9, 2022
1195f44
Disable GitHub Actions fail-fast strategy
ifox Feb 9, 2022
de97a56
Refactor deprecated PHPUnit annotations
ifox Feb 10, 2022
bcfbcce
Merge pull request #546 from area17/v6
lazychaser Feb 11, 2022
81f9ecf
Merge pull request #542 from doegel/nova-fix
lazychaser Feb 11, 2022
350b3c5
Merge pull request #512 from vokamut/patch-1
lazychaser Feb 11, 2022
49490dd
Fix return type of NodeTrait::scoped method
sforward Jun 28, 2022
a9fbb01
[6.x] support Laravel 10.x
jonnott Feb 15, 2023
95f5839
Merge pull request #578 from jonnott/v6-l10
lazychaser Feb 16, 2023
2d5c99f
Update
lazychaser Feb 16, 2023
562f5e1
fix readme versions
jonnott Feb 16, 2023
bf599ab
eagerLoadingAncestorsWithScope | add failing test
kvas-damian Jun 22, 2023
df8736a
eagerLoadingAncestorsWithScope | fix applying scope in whereAncestorO…
kvas-damian Jun 22, 2023
38d3bd4
Merge pull request #586 from bethinkpl/fix-eagerLoadingAncestorsWithS…
lazychaser Nov 29, 2023
324d497
Merge pull request #580 from jonnott/patch-1
lazychaser Nov 29, 2023
815b15b
Merge pull request #562 from sforward/fix/return-type
lazychaser Nov 29, 2023
f7a36eb
Update readme
lazychaser Nov 29, 2023
c16faeb
Drop compatibility for EOL PHP versions, update dependecies for PHP 8…
asurcodes Mar 19, 2024
8600311
Update readme
asurcodes Mar 19, 2024
dea625c
Merge branch 'master' into v6
asurcodes Mar 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Unit Tests

on:
push:
branches:
- master
pull_request:
branches:
- "*"
schedule:
- cron: '0 0 * * *'

jobs:
php-tests:
runs-on: ubuntu-latest
timeout-minutes: 15
env:
COMPOSER_NO_INTERACTION: 1

strategy:
fail-fast: false
matrix:
php: [8.3, 8.2]

name: P${{ matrix.php }}

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: none
tools: composer:v2

- name: Install dependencies
run: |
composer install -o --quiet

- name: Execute Unit Tests
run: composer test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
composer.phar
composer.lock
.DS_Store
.phpunit.result.cache
12 changes: 0 additions & 12 deletions .travis.yml

This file was deleted.

14 changes: 12 additions & 2 deletions CHANGELOG.markdown
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
### 4.2.8
### 4.3.4
* Support Laravel 5.8

### 4.3.3
* Support Laravel 5.7

### 4.3.2
* Support Laravel 5.6
* Added `nestedSet` and `dropNestedSet` blueprint macros

### 4.3.0
* Support Laravel 5.5
* Added `fixSubtree` and `rebuildSubtree` methods
* Increased performance of tree rebuilding
Expand Down Expand Up @@ -104,4 +114,4 @@
### 1.1.0

* `Collection::toDictionary` is now obsolete. Use `Collection::groupBy`.
* Laravel 4.2 is required
* Laravel 4.2 is required
51 changes: 35 additions & 16 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
[![Latest Unstable Version](https://poser.pugx.org/kalnoy/nestedset/v/unstable.svg)](https://packagist.org/packages/kalnoy/nestedset)
[![License](https://poser.pugx.org/kalnoy/nestedset/license.svg)](https://packagist.org/packages/kalnoy/nestedset)

This is a Laravel 4-5 package for working with trees in relational databases.

* **Laravel 5.2, 5.3, 5.4, 5.5** is supported since v4
This is a Laravel 4-11 package for working with trees in relational databases.

* **Laravel 11.0** is supported since v7
* **Laravel 10.0** is supported since v6.0.2
* **Laravel 9.0** is supported since v6.0.1
* **Laravel 8.0** is supported since v6.0.0
* **Laravel 5.7, 5.8, 6.0, 7.0** is supported since v5
* **Laravel 5.5, 5.6** is supported since v4.3
* **Laravel 5.2, 5.3, 5.4** is supported since v4
* **Laravel 5.1** is supported in v3
* **Laravel 4** is supported in v2

Although this project is completely free for use, I appreciate any support!

- __[Donate via PayPal](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=5TJUM7FYU5VR2)__
- My Visa: 4276 0700 1073 4244

__Contents:__

- [Theory](#what-are-nested-sets)
Expand Down Expand Up @@ -384,6 +385,9 @@ position.
Various constraints that can be applied to the query builder:

- __whereIsRoot()__ to get only root nodes;
- __hasParent()__ to get non-root nodes;
- __whereIsLeaf()__ to get only leaves;
- __hasChildren()__ to get non-leave nodes;
- __whereIsAfter($id)__ to get every node (not just siblings) that are after a node
with specified id;
- __whereIsBefore($id)__ to get every node that is before a node with specified id.
Expand Down Expand Up @@ -571,17 +575,17 @@ protected function getScopeAttributes()
}
```

But now in order to execute some custom query, you need to provide attributes
But now, in order to execute some custom query, you need to provide attributes
that are used for scoping:

```php
MenuItem::scoped([ 'menu_id' => 5 ])->withDepth()->get(); // OK
MenuItem::descendantsOf($id)->get(); // WRONG: returns nodes from other scope
MenuItem::scoped([ 'menu_id' => 5 ])->fixTree();
MenuItem::scoped([ 'menu_id' => 5 ])->fixTree(); // OK
```

When requesting nodes using model instance, scopes applied automatically based
on the attributes of that model. See examples:
on the attributes of that model:

```php
$node = MenuItem::findOrFail($id);
Expand All @@ -595,12 +599,13 @@ To get scoped query builder using instance:
$node->newScopedQuery();
```

Note, that scoping is not required when retrieving model by primary key
(since the key is unique):
#### Scoping and eager loading

Always use scoped query when eager loading:

```php
$node = MenuItem::findOrFail($id); // OK
$node = MenuItem::scoped([ 'menu_id' => 5 ])->findOrFail(); // OK, but redundant
MenuItem::scoped([ 'menu_id' => 5])->with('descendants')->findOrFail($id); // OK
MenuItem::with('descendants')->findOrFail($id); // WRONG
```

Requirements
Expand All @@ -625,7 +630,21 @@ composer require kalnoy/nestedset

#### The schema

You can use a method to add needed columns with default names:
For Laravel 5.5 and above users:

```php
Schema::create('table', function (Blueprint $table) {
...
$table->nestedSet();
});

// To drop columns
Schema::table('table', function (Blueprint $table) {
$table->dropNestedSet();
});
```

For prior Laravel versions:

```php
...
Expand Down
32 changes: 25 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kalnoy/nestedset",
"description": "Nested Set Model for Laravel 4-5",
"description": "Nested Set Model for Laravel 5.7 and up",
"keywords": ["laravel", "nested sets", "nsm", "database", "hierarchy"],
"license": "MIT",

Expand All @@ -12,10 +12,10 @@
],

"require": {
"php": ">=5.5.9",
"illuminate/support": "5.2 - 5.5",
"illuminate/database": "5.2 - 5.5",
"illuminate/events": "5.2 - 5.5"
"php": "^8.2",
"illuminate/support": "^11.0",
"illuminate/database": "^11.0",
"illuminate/events": "^11.0"
},

"autoload": {
Expand All @@ -24,15 +24,33 @@
}
},

"autoload-dev": {
"psr-4": {
"Kalnoy\\Nestedset\\Tests\\": "tests/"
}
},

"require-dev": {
"phpunit/phpunit": "4.8.*"
"phpunit/phpunit": "10.*"
},

"minimum-stability": "dev",
"prefer-stable": true,

"extra": {
"branch-alias": {
"dev-master": "v4.2.x-dev"
"dev-master": "v5.0.x-dev"
},

"laravel": {
"providers": [
"Kalnoy\\Nestedset\\NestedSetServiceProvider"
]
}
},
"scripts": {
"test": [
"@php ./vendor/bin/phpunit"
]
}
}
3 changes: 2 additions & 1 deletion phpunit.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
$capsule->bootEloquent();
$capsule->setAsGlobal();

include __DIR__.'/tests/models/Category.php';
include __DIR__.'/tests/models/Category.php';
include __DIR__.'/tests/models/MenuItem.php';
45 changes: 21 additions & 24 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="phpunit.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="true"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./src</directory>
</whitelist>
</filter>
</phpunit>
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
backupGlobals="false"
bootstrap="phpunit.php"
colors="true"
processIsolation="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
cacheDirectory=".phpunit.cache"
backupStaticProperties="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>./src</directory>
</include>
</source>
</phpunit>
3 changes: 2 additions & 1 deletion src/AncestorsRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public function addConstraints()
{
if ( ! static::$constraints) return;

$this->query->whereAncestorOf($this->parent)->defaultOrder();
$this->query->whereAncestorOf($this->parent)
->applyNestedSetScope();
}

/**
Expand Down
37 changes: 16 additions & 21 deletions src/BaseRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,28 +111,15 @@ public function initRelation(array $models, $relation)
return $models;
}

/**
* @param EloquentBuilder $query
* @param EloquentBuilder $parent
* @param array $columns
*
* @return mixed
*/
public function getRelationQuery(
EloquentBuilder $query, EloquentBuilder $parent,
$columns = [ '*' ]
) {
return $this->getRelationExistenceQuery($query, $parent, $columns);
}

/**
* Get a relationship join table hash.
*
* @param bool $incrementJoinCount
* @return string
*/
public function getRelationCountHash()
public function getRelationCountHash($incrementJoinCount = true)
{
return 'nested_set_'.self::$selfJoinCount++;
return 'nested_set_'.($incrementJoinCount ? static::$selfJoinCount++ : static::$selfJoinCount);
}

/**
Expand All @@ -154,10 +141,6 @@ public function getResults()
*/
public function addEagerConstraints(array $models)
{
$model = reset($models);

$this->query = $model->newScopedQuery();

$this->query->whereNested(function (Builder $inner) use ($models) {
// We will use this query in order to apply constraints to the
// base query builder
Expand Down Expand Up @@ -207,4 +190,16 @@ protected function matchForModel(Model $model, EloquentCollection $results)

return $result;
}
}

/**
* Get the plain foreign key.
*
* @return mixed
*/
public function getForeignKeyName()
{
// Return a stub value for relation
// resolvers which need this function.
return NestedSet::PARENT_ID;
}
}
3 changes: 2 additions & 1 deletion src/DescendantsRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public function addConstraints()
{
if ( ! static::$constraints) return;

$this->query->whereDescendantOf($this->parent);
$this->query->whereDescendantOf($this->parent)
->applyNestedSetScope();
}

/**
Expand Down
Loading