Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

warpig's avatar
Level 12

syntax error, unexpected token "endforeach", expecting "elseif" or "else" or "endif"

i created a component for my categories and subcategories

<?php

namespace App\View\Components;

use Closure;
use App\Models\Category;
use Illuminate\View\Component;
use Illuminate\Contracts\View\View;

class CategoryComponent extends Component
{
    /**
     * Create a new component instance.
     */
    public $categories;

    public function __construct(Category $categories)
    {
        $this->categories = Category::whereNull('parent_id')
            ->with('subCategory')
            ->get();
    }

    /**
     * Get the view / contents that represent the component.
     */
    public function render(): View|Closure|string
    {
        return view('components.categories', [
            'categories' => $this->categories,
        ]);
    }
}
<div class="flex-col items-center justify-center w-full py-4" x-data="{ open: false }">
    <a href="{{ route('by-category', $category) }}" @mouseenter="open = true" @mouseleave="open = false" class="text-black font-bold uppercase">
        {{ $category->title }} <i :class="open ? 'fa-chevron-down': 'fa-chevron-up'" class="fas ml-2"></i>
    </a>
    <div :class="open ? 'block': 'hidden'" @mouseenter="open = true" @mouseleave="open = false" class="bg-black">
        @foreach($category->subCategory as $subCategory)  
            <a href="{{ route('by-category', $subCategory) }}" class="text-white text-lg transition ease-out hover:bg-gray-100 hover:text-black rounded py-2 px-4 mx-2">
                {{ $subCategory->title }}
            </a>
        @endforeach
    </div>
</div>
    @foreach ($categories as $category)
        <x-categories :category="$category"></categories/>
    @endforeach

the only time this worked was when the function was inside tof the PostsController:

    public function index()
    {
        $posts = Post::query()
            ->where('active', '=', 1)
            ->where('published_at', '<', Carbon::now())
            ->orderBy('published_at', 'desc')
            ->with('categories')
            ->take(4)
            ->get();

        $latestPost = $posts->shift();
     
        return view('home', compact('posts', 'latestPost'));
    }

And one of those other methods that this controller had was "by-category" which is something im using to route my categories on to a new page that would display all categories, this would be it:

Route::get('/category/{category:slug}', [PostController::class, 'byCategory'])->name('by-category');
    public function byCategory(Category $category)
    {
        $categories = Category::query();

        $posts = Post::query()
            ->join('category_post', 'posts.id', '=', 'category_post.post_id')
            ->where('category_post.category_id', '=', $category->id)
            ->where('active', '=', true)
            ->whereDate('published_at', '<=', Carbon::now())
            ->orderBy('published_at', 'desc')
            ->paginate(10);

            return view('categories', compact('posts', 'categories', 'category'));
    }

at the moment im receiving this error : https://flareapp.io/share/V7jKAWO5#F67

what are my options? how can i reuse the code that relates to the categories from this controller to the category component? thanks

0 likes
2 replies
warpig's avatar
Level 12

ive made a categorytrait just not sure how its supposed to be used in the category component? or if i even need to do something else? as in my postscontroller and category model all i did was to use CategoryTrait; at the top. not sure if im doing it correctly. This is the trait

<?php

namespace App\Traits;

use App\Models\Post;
use App\Models\Category;
use Illuminate\Support\Carbon;

trait CategoryTrait {
    public function byCategory(Category $category)
    {
        $categories = Category::query();

        $posts = Post::query()
            ->join('category_post', 'posts.id', '=', 'category_post.post_id')
            ->where('category_post.category_id', '=', $category->id)
            ->where('active', '=', true)
            ->whereDate('published_at', '<=', Carbon::now())
            ->orderBy('published_at', 'desc')
            ->paginate(10);

            return view('categories', compact('posts', 'categories', 'category'));
    }

    public function index()
    {
        $posts = Post::query()
            ->where('active', '=', 1)
            ->where('published_at', '<', Carbon::now())
            ->orderBy('published_at', 'desc')
            ->with('categories')
            ->take(4)
            ->get();

        $latestPost = $posts->shift();
     
        return view('home', compact('posts', 'latestPost'));
    }
}

everything works as per usual, just that im still getting the same syntax error.

1 like
tisuchi's avatar
tisuchi
Best Answer
Level 70

@warpig I think it's the problem. The correct closing tag should be </x-categories> instead of </categories/>.

Try with this:

@foreach ($categories as $category)
    <x-categories :category="$category"></x-categories>
@endforeach
1 like

Please or to participate in this conversation.