Issue with Nested CRUD & setAccessCondition #1408
Replies: 1 comment
-
|
You're correct in identifying that Backpack's getCurrentEntryId() can return the wrong parameter (e.g., user_id instead of post_id) in nested CRUD scenarios, especially during create operations where no entry exists yet. This is a known issue and has been discussed and worked around by the community in several places. The ProblemWhen using setAccessCondition (or similar logic that relies on getCurrentEntryId()), Backpack tries to determine the current entry's ID from the route parameters. In nested resources, the last parameter may not always be the correct entity's ID—during create, it might be user_id instead of post_id, leading to incorrect behavior or errors. Community WorkaroundsSeveral workarounds have been suggested and used:
Official Patch and PRsThere is an official PR (Get the correct current ID for nested resources) that aims to address this by improving how the current entry ID is determined for nested resources. However, the underlying challenge remains: Backpack must guess which parameter is the actual entity ID, and this can be ambiguous in complex nested routes. Overriding getCurrentEntryIdIf you want to override getCurrentEntryId, you can do so by extending the CrudPanel or by using a trait in your CrudController. For example: // In your CrudController
public function getCurrentEntryId()
{
if ($this->entry) {
return $this->entry->getKey();
}
if ($this->getOperation() === 'create') {
return null; // or false, depending on your needs
}
$params = \Route::current()?->parameters() ?? [];
// Your custom logic here
return $this->getRequest()->input($this->entity_name) ??
array_values($params)[count($params) - 1] ??
null;
}You can also use a trait to share this logic across multiple controllers if needed. Summary
If you need to apply this fix project-wide, a trait is the cleanest approach. If you only need it in one controller, override the method directly there. Let me know if you need a more specific code example for your use case! ℹ️ Please note our AI Bot does not reply to follow-ups, this will be the only AI answer in this thread. If the AI helped you find the answer, please mark it as answered or write details below so it can learn. Then close the conversation. If not, please give more information for when a human reads this. ℹ️ Relevant sources: Github Issue (closed): Previous CrudController edit method functionality is now broken |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I encountered a problem using
setAccessConditionand a Nested CRUD (e.g.: user's posts) like in the tutorial https://backpackforlaravel.com/articles/tutorials/how-to-setup-nested-crud, AND setAccessConditionWith Nested Crud I have:
admin/user/{user_id}/post/{post_id}/show
admin/user/{user_id}/post/create
setAccessCondition calls getCurrentEntryId() in vendor/backpack/crud/src/app/Library/CrudPanel/Traits/Read.php
When creating, there is no current entry so it tries to find the post_id parameter (array_values($params)[count($params) - 1] ) but wrongly gets the user_id
My solution is to add and extra check.
Is there a better way?
How can I override getCurrentEntryId?
Beta Was this translation helpful? Give feedback.
All reactions