lazychaser / laravel-nestedset

Effective tree structures in Laravel 4-8

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Descendants Relationship doesn't work

ComputerMaverick69 opened this issue · comments

I have created a $node like $node = new Category(); and have saved the category. I then use the parent's children relationship to create a child node like this $parent->children()->create($attributes); This gets properly saved in the database but when i use the descendants relationship it shows an empty array but in the database, it shows the parent_id column as having a parent.

$categories = Category::with('descendants')->select([
            'title',
            'subtitle',
            'img',
            'status'
        ])->whereNull('parent_id')->get();

//In blade I do
@foreach ($categories as $i => $subCategory)
      <tr>
          <td>{{ $subCategory->descendants->pluck('title') }}</td>
          <td>{{ $subCategory->descendants->pluck('subtitle') ?? 'N/A' }}</td>
          <td>{{ $subCategory->title ?? 'N/A' }}</td>
          <td>@if($subCategory->descendants->pluck('img') == null) N/A @else <img src="{{ $subCategory->descendants->pluck('img') }}" width="30" />  @endif</td>
          <td>{{ ucfirst($subCategory->descendants->pluck('status')) }}</td>
          <td>
              <a href="{{ route('sub-categories.edit', $subCategory->descendants->pluck('id')) }}" class="btn btn-sm btn-info btn-b">
                  <i class="las la-pen"></i>
              </a>
              <form action="{{ route('sub-categories.destroy', $subCategory->descendants->pluck('id')) }}" method="post">
                  @csrf
                  @method('DELETE')
                  <button type="submit" class="btn btn-sm btn-danger"><i class="las la-trash"></i></button>
              </form>
          </td>
      </tr>
@endforeach

It comes off a a blank array []

Screenshot 2022-05-05 at 11 18 41 AM

Screenshot 2022-05-05 at 11 19 40 AM

I can reproduce this by updating mysql from 8.0.28 to 8.0.29.
Are you using mysql?

Yes i am

I can reproduce this by updating mysql from 8.0.28 to 8.0.29. Are you using mysql?

Changing from a View to a Table solved my issue