Nested Set include parent relationship #1001
-
I tried using this package with Nested Set but I have trouble including the parent relationship. The requests all go through but the parent relationship is never included with the response. Any help is appreciated since I have no idea why this happens. Note that other relationships from Nested Set work if defined, for example ancestors, which includes the parent model. This is a minimal example with a new Laravel 12 application, all other files are unmodified. ~/Projects/minimal> composer require kalnoy/nestedset spatie/laravel-query-builder <?php // app/Models/Nested.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Kalnoy\Nestedset\NodeTrait;
class Nested extends Model
{
use NodeTrait;
} <?php // routes/web.php
use App\Models\Nested;
use Illuminate\Support\Facades\Route;
use Spatie\QueryBuilder\QueryBuilder;
Route::get('/', function () {
return QueryBuilder::for(Nested::class)
->allowedIncludes(['parent'])
->get();
}); <?php // database/migrations/0001_01_01_000003_create_nested_table.php
use App\Models\Nested;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('nesteds', function (Blueprint $table) {
$table->id();
$table->nestedSet();
$table->timestamps();
});
$parent = Nested::create();
for ($i = 0; $i < 10; ++$i) {
$child = Nested::create();
$child->parent_id = $parent->id;
$child->save();
}
Nested::fixTree();
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('nesteds');
}
}; And with all that setup if I run run the server and query it I get the following result without the parent model. ~/Projects/minimal> http get --headers [Accept application/json] http://localhost:8001?include=parent
╭────┬────┬──────┬──────┬───────────┬─────────────────────────────┬─────────────────────────────╮
│ # │ id │ _lft │ _rgt │ parent_id │ created_at │ updated_at │
├────┼────┼──────┼──────┼───────────┼─────────────────────────────┼─────────────────────────────┤
│ 0 │ 1 │ 1 │ 22 │ │ 2025-04-09T13:29:53.000000Z │ 2025-04-09T13:29:53.000000Z │
│ 1 │ 2 │ 2 │ 3 │ 1 │ 2025-04-09T13:29:53.000000Z │ 2025-04-09T13:29:53.000000Z │
│ 2 │ 3 │ 4 │ 5 │ 1 │ 2025-04-09T13:29:53.000000Z │ 2025-04-09T13:29:53.000000Z │
│ 3 │ 4 │ 6 │ 7 │ 1 │ 2025-04-09T13:29:53.000000Z │ 2025-04-09T13:29:53.000000Z │
│ 4 │ 5 │ 8 │ 9 │ 1 │ 2025-04-09T13:29:53.000000Z │ 2025-04-09T13:29:53.000000Z │
│ 5 │ 6 │ 10 │ 11 │ 1 │ 2025-04-09T13:29:53.000000Z │ 2025-04-09T13:29:53.000000Z │
│ 6 │ 7 │ 12 │ 13 │ 1 │ 2025-04-09T13:29:53.000000Z │ 2025-04-09T13:29:53.000000Z │
│ 7 │ 8 │ 14 │ 15 │ 1 │ 2025-04-09T13:29:53.000000Z │ 2025-04-09T13:29:53.000000Z │
│ 8 │ 9 │ 16 │ 17 │ 1 │ 2025-04-09T13:29:53.000000Z │ 2025-04-09T13:29:53.000000Z │
│ 9 │ 10 │ 18 │ 19 │ 1 │ 2025-04-09T13:29:53.000000Z │ 2025-04-09T13:29:53.000000Z │
│ 10 │ 11 │ 20 │ 21 │ 1 │ 2025-04-09T13:29:53.000000Z │ 2025-04-09T13:29:53.000000Z │
╰────┴────┴──────┴──────┴───────────┴─────────────────────────────┴─────────────────────────────╯ |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Found out this is a problem with Nested Set and not Query Builder. |
Beta Was this translation helpful? Give feedback.
Found out this is a problem with Nested Set and not Query Builder.
lazychaser/laravel-nestedset#618