mardi 13 avril 2021

Blade recursive view showing parent > child page as well as child page

Hard to describe this in the title so this image might clarify:

Multi select dropdown

I have a recursive view which outputs the above:

@foreach($pages as $page)
    <option value=""
            @if($selected == $page->id) selected @endif
    >@for($i=0; $i<$levels; $i++)&#8618;@endfor  (#)
    </option>

    @if($page->children->count())
        @include('cms::admin.partials.page-selector-option', ['pages' => $page->children, 'levels' => $levels + 1])
    @endif
@endforeach

Let's say that a page has the following columns:

  • id
  • parent_id
  • title
  • content
  • template_id

The problem is that the collection of pages passed to this view e.g:

$pages = Page::where('template_id', 10)->get()

Contains a page with a child page of 'test subcat' which is being output as a child page correctly, but it also contains the subpage itself being output as a new item below (see both #54 pages loading in the screenshot).

Doing something like this:

$pages = Page::where('template_id', 10)->where('parent_id', 0)->get()

Does help, however I still want to show pages here that are not necessarily top level pages so may not have a parent_id of 0.

What I think I need to do is query the parent() relationship to see if the parent_id has the same template_id and if it does then do not return this item. Here's what I have so far which doesn't work:

  public function scopeWithoutChildren($query)
    {
        return $query->with(
            [
                'parent' => function ($q) {
                    $q->where('template_id', '!=', 10);
                }
            ]);
    }


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/328Solr
via IFTTT

Aucun commentaire:

Enregistrer un commentaire