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

shahr's avatar
Level 10

Method Illuminate\Database\Eloquent\Collection::path does not exist.

I installed "cviebrock/eloquent-sluggable": "^4.8", and I want to add slug path of committees category.

I have a many committees in my menu

committees

Look at my codes,

AppServiceProvider.php

public function register()
{
    Schema::defaultStringLength(191);
    view()->composer('*', function($view) {
        $view->with('catCommittee', Category::where('parent_id',16)->get());
    });
}

header.blade.php

<li class="nav-item dropdown has-mega-menu position-static">
    <a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button"
       aria-haspopup="true" aria-expanded="false">
        {{ __('message.menu.committees') }}
    </a>

    <div class="dropdown-menu w-100">
        <div class="px-0 container">
            <div class="row">
                @if (isset($catCommittee))
                    @foreach($catCommittee->split($catCommittee->count()/1) as $row)
                        <div class="col-md-4">
                            @foreach($row as $committee)
                                @if(app()->getLocale() == 'fa')
                                    <a class="dropdown-item text-right"
                                       href="{{ $committee->committees->path() }}">{{ $committee->name }}</a>
                                @else
                                    <a class="dropdown-item text-right"
                                       href="{{ $committee->committees->path() }}">{{ $committee->title_en }}</a>
                                @endif
                            @endforeach
                        </div>
                    @endforeach
                @endif
            </div>
        </div>
    </div>
</li>

Category.php

public function committees()
{
    return $this->belongsToMany(Committee::class,'category_committee','category_id','committee_id');
}

Committee.php

public function path()
{
    $locale = app()->getLocale();
    return "/$locale/committee/$this->slug";
}

I get this error...

Method Illuminate\Database\Eloquent\Collection::path does not exist.

0 likes
34 replies
shahr's avatar
Level 10

No one can answer my question?

frankielee's avatar

Seems like the error is due this, the committees is an array, should it be $committee->path() or $committee->committees[0]->path() instead of $committee->committees->path()

  <a class="dropdown-item text-right"
                                       href="{{ $committee->committees->path() }}"
1 like
shahr's avatar
Level 10

I replaced it

<a class="dropdown-item text-right" href='{{ $committee->committees[0]->path() }}'>

But my problem did not solve yet. I see this error.

error

frankielee's avatar

Are there any $committee->committees that are empty? Could you try this way, comments the codes

@if(app()->getLocale() == 'fa')
                                    //<a class="dropdown-item text-right"
                                     //  href="{{ $committee->committees->path() }}">{{ $committee->name }}</a>
                                //@else
                              //      <a class="dropdown-item text-right"
                              //         href="{{ $committee->committees->path() }}">{{ $committee->title_en }}</a>
{{$committee->committees ?? "empty" }}
@endif
1 like
shahr's avatar
Level 10

I replaced it

@if(app()->getLocale() == 'fa')
{{--<a class="dropdown-item text-right" href='{{ $committee->committees[0]->path() }}'>--}}
{{--{{ $committee->name }}--}}
{{--</a>--}}
{{--@else--}}
{{--<a class="dropdown-item text-left" href="{{ $committee->slug }}">--}}
{{--{{ $committee->title_en }}--}}
{{--</a>--}}
{{ $committee->committees ?? "empty" }}
@endif

I see this emo

menu

Snapey's avatar

The error message

Method Illuminate\Database\Eloquent\Collection::path does not exist.

tells you that you have a collection That is, there is more than one model. You cannot call the path method on a collection of models, only on a single model.

You either need to iterate over the collection and get the path for each entity or alter your query so that you only get a single model and not a collection.

Snapey's avatar

Also, you might want to check your database queries because using * in a view composer is NEVER a good idea.

shahr's avatar
Level 10

@Snapey

What's the solution? Can you put your code snippet here for example?

frankielee's avatar

You will need to apply the foreach loop on the collection

@foreach($committee->committees as $item)
if(app()->getLocale() == 'fa')
                                    <a class="dropdown-item text-right"
                                       href="{{ $item->path() }}">{{ $committee->name }}</a>
                                @else
                                    <a class="dropdown-item text-right"
                                       href="{{$item->path() }}">{{ $committee->title_en }}</a>
@endif

@endforeach
1 like
Snapey's avatar

No because

a) I don't know what you try to do

b) I don't know how your models relate to each other

Snapey's avatar

If committee has name and title, why does it not have path ? why are you referring to $item ?

shahr's avatar
Level 10

@frankielee

I see this error.

error

<div class="dropdown-menu w-100">
    <div class="px-0 container">
        <div class="row">
            @if (isset($catCommittee))
                @foreach($catCommittee->split($catCommittee->count()/1) as $row)
                    <div class="col-md-4">
                        @foreach($committee->committees as $item)
                            @if(app()->getLocale() == 'fa')
                            <a class="dropdown-item text-right"
                               href="{{ $item->path() }}">{{ $committee->name }}</a>
                            @else
                                <a class="dropdown-item text-right"
                                   href="{{$item->path() }}">{{ $committee->title_en }}</a>
                            @endif

                        @endforeach
                    </div>
                @endforeach
            @endif
        </div>
    </div>
</div>
frankielee's avatar
Level 29

@oxbir you are missing @foreach($row as $committee)

@foreach($row as $committee)
@foreach($committee->committees as $item)
                            @if(app()->getLocale() == 'fa')
                            <a class="dropdown-item text-right"
                               href="{{ $item->path() }}">{{ $committee->name }}</a>
                            @else
                                <a class="dropdown-item text-right"
                                   href="{{$item->path() }}">{{ $committee->title_en }}</a>
                            @endif

                        @endforeach 
@endforeach
1 like
frankielee's avatar

@snapey, I know what you mean, but I will just try to solve his issue with the codes he showed. Else it will be another issue for refracturing the code.

frankielee's avatar

Perhaps the name or title is empty, try

            @if(app()->getLocale() == 'fa')
                            <a class="dropdown-item text-right"
                               href="{{ $item->path() }}">{{ $committee->name ?? "Is Empty"}}</a>
                            @else
                                <a class="dropdown-item text-right"
                                   href="{{$item->path() }}">{{ $committee->title_en ?? "Is Empty" }}</a>
                            @endif

shahr's avatar
Level 10

I replaced prev codes , but it did not show Is Empty.

frankielee's avatar

Maybe you need to check the model has the name or not

                           <a class="dropdown-item text-right"
                               href="{{ $item->path() }}">Name :{{ $committee}}</a>
                            @else
                                <a class="dropdown-item text-right"
                                   href="{{$item->path() }}">Title :{{ $committee}}</a>
shahr's avatar
Level 10

I see this.

I have name and title_en in categories table.

public function up()
{
    Schema::create('categories', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->string('title_en');
        $table->text('description');
        $table->bigInteger('parent_id')->default(0);
        $table->string('lang');
        $table->bigInteger('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->timestamps();
    });
}

eee

frankielee's avatar

The issue is due to this code <div class="col-md-4">, try to validate the $rowlength is > 0 before applying the rest

Example

@foreach($catCommittee->split($catCommittee->count()/1) as $row)
		 @if(count($row)>0)	
                    <div class="col-md-4">
			your codes

		@endif 	



shahr's avatar
Level 10

I see this demo again.

committees

this is my codes

<div class="dropdown-menu w-100">
                                <div class="px-0 container">
                                    <div class="row">
                                        @if (isset($catCommittee))
                                            @foreach($catCommittee->split($catCommittee->count()/1) as $row)
                                                @if(count($row)>0)
                                                    <div class="col-md-4">
                                                        @foreach($row as $committee)
                                                            @foreach($committee->committees as $item)
                                                                @if(app()->getLocale() == 'fa')
                                                                    <a class="dropdown-item text-right"
                                                                       href="{{ $item->path() }}">Name :{{ $committee->name }}</a>
                                                                @else
                                                                    <a class="dropdown-item text-right"
                                                                       href="{{$item->path() }}">Title :{{ $committee->title_en }}</a>
                                                                @endif
                                                            @endforeach
                                                        @endforeach
                                                    </div>
                                                @endif
                                            @endforeach
                                        @endif
                                    </div>
                                </div>
                    </div>
shahr's avatar
Level 10

How to I see all of the categories.

frankielee's avatar

Try this, move the <div class="col-md-4"> into the loop @foreach($committee->committees as $item)

@foreach($committee->committees as $item)
<div class="col-md-4">//here
                                                                @if(app()->getLocale() == 'fa')
                                                                    <a class="dropdown-item text-right"
                                                                       href="{{ $item->path() }}">Name :{{ $committee->name }}</a>
                                                                @else
                                                                    <a class="dropdown-item text-right"
                                                                       href="{{$item->path() }}">Title :{{ $committee->title_en }}</a>
                                                                @endif
                                                            @endforeach 
frankielee's avatar

https://imgur.com/TGoJoVx

By referring to the image, those blanks are due to the empty value. Try removing all the <div class="col-md-4"> and move to the if loop

@foreach($committee->committees as $item)

                                                                @if(app()->getLocale() == 'fa')
<div class="col-md-4">
                                                                    <a class="dropdown-item text-right"
                                                                       href="{{ $item->path() }}">Name :{{ $committee->name }}</a>
</div>
                                                                @else
<div class="col-md-4">
                                                                    <a class="dropdown-item text-right"
                                                                       href="{{$item->path() }}">Title :{{ $committee->title_en }}</a>
</div>
                                                                @endif
                                                            @endforeach 
frankielee's avatar

Have you tried to clear cached and hard reload? Could you try to move the <div class="col-md-4"> to different location? It's hard for me to debug for you here since I can't access the code directly

shahr's avatar
Level 10

@michaloravec

I want to create 3 columns. That's why I used split.

@frankielee

Thank you, I did all of them but I still haven't succeeded,

HOW TO SEE ALL CATEGORIES IN MENU...

MichalOravec's avatar

@oxbir When you want to split to three columns so why do you use?

@foreach($catCommittee->split($catCommittee->count()/1) as $row)

Instead that use ->split(3)

@foreach($catCommittee->split(3) as $row)

By the way, whatever / 1 it's everytime just whatever

1 like
Next

Please or to participate in this conversation.