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

beerbuddha's avatar

Nova BelongsToMany breaks memory cap

In Nova 1.2.0 / Laravel 5.7.14

I have a BelongsToMany that is breaking the php memory when it is being pulled. I have a resource that I can see the count of (over 30000) from a BelongsToMany - when I click to the detail of the resource - it tells me that it hit a memory cap.

According to the doc i can only find: https://nova.laravel.com/docs/1.0/resources/relationships.html#morphtomany

Searchable relation

HOWEVER - this solution is only for Create/Update - I need one for the Detail. When it finish rendering - I will get pagination but the page won't load at all. Any idea for solution?

I tried using telescope but to no avail to solve this.

As an example I have a Thread class on index I can see how many users and on Detail I hook myself with a belongstomany

            Number::make('Users', function () {
                return $this->userCount();
            })
                ->hideWhenCreating()
                ->hideWhenUpdating(),   
            BelongsToMany::make('Users', 'users', Users::class)->fields(function () {
                return [
                    Text::make('weight', 'value'),
                ];
            }),

It just break the detail page

0 likes
10 replies
aurawindsurfing's avatar

Will you really browse those 30000 relations? I think you can do 2 things:

1: Increase memory - probably not best idea. 2: Limit your query since it is unlikely that you need all of it in your memory.

If we are talking about your index you can also show it as Text::make field and not a relationship field.

Can you also give an example of what you trying to do exactly?

beerbuddha's avatar

@AURAWINDSURFING - I think you are miss-reading the problem.

The index is not an issue here unless and only if, i try to eager load the relationship. the problem is in the detail resource. Within a given detail resource if it has a belongstomany relationship resource it will tap out on the memory, however during display it only shows 5.

No i am not actually interested in seeing all the 30k relations but Nova automatically loads all of them despite only showing 5 at a time with pagination - that is the real issue here.

aurawindsurfing's avatar

I see your point. I would say try to show it without the relationship maybe? Or use quotes to limit if you really need to show it with the relationship.

Increasing the memory should do the trick for sure but the better way would be to get rid of that bloated query for sure.

beerbuddha's avatar

@AURAWINDSURFING - yeah im trying NOT to increase the memory because users will keep coming in.

to replicate the situation its pretty easy. have a belongstomany relationship with any given class and load up 30k attach to the relationship (think 1 thread belongstomany users) then factory 30k users to it

the resource won't load.

No i need to show the resource with the relationship or else it defeats the purpose. The problem there are currently (as far as i can tell) a way to only load the limited amount with pagination despite visually it limits it and uses pagination

aurawindsurfing's avatar

Try to search javascript forums for similar problems since it is clearly a SPA / Vue issue and not really Laravel issue.

aurawindsurfing's avatar

Well do you hit the same problem when you do the same thing in your laravel controller? If not then it is javascript issue and not php issue IMHO.

What is the error exactly?

beerbuddha's avatar

@AURAWINDSURFING - the exact error:

"Allowed memory size of 134217728 bytes exhausted (tried to allocate 14680072 bytes)","code":1,"file":"/app/vendor/laravel/framework/src/Illuminate/Database/Connection.php:330"
bfj5889's avatar

Did you ever find a solution to this problem?

I have a user_permission table for my Users and Permissions model and want to be able to show the permissions for a user when you show the user but the relationship table has over 100,000 records in it and am getting the same memory error. Seems like a major flaw to not be able to safely handle relationship tables with a mass amount of data :/

ntimyeboah's avatar

@bfj5889 You can add the searchable() method to the field so it doesn't load all the relationships at once

2 likes

Please or to participate in this conversation.