How to add pivot field without use repeatable? #1198
-
Hello, I have created a field that read the content from a pivot table:
This is the relationship:
Basically I need to pass the "user_id" to the pivot table called "user_permission_contents" which have the following columns: content_id, content_type, user_id.
I also get a repeatable structure, but I just need a select 2 with a list of Page. How can I pass the user_id field to the pivot table? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
To answer your question about passing the
public function permissionPages()
{
return $this->belongsToMany(
Page::class,
'user_permission_contents',
'content_id',
'id',
)->withPivot('user_id');
}
CRUD::field([
'name' => 'permissionPages',
'type' => 'relationship',
'tab' => $permissions_tab,
'subfields' => [
[
'name' => 'user_id',
'type' => 'hidden',
'value' => backpack_user()->id, // Assuming you're using Backpack's authentication
]
],
]); This configuration will:
The For more details on how to use the If you need to customize the options shown in the select2 list or add any other configurations, you can add them to the field definition. For example: CRUD::field([
'name' => 'permissionPages',
'type' => 'relationship',
'tab' => $permissions_tab,
'attribute' => 'page_title', // Assuming the Page model has a 'page_title' attribute
'placeholder' => 'Select pages',
'subfields' => [
[
'name' => 'user_id',
'type' => 'hidden',
'value' => backpack_user()->id,
]
],
]); This approach should allow you to pass the |
Beta Was this translation helpful? Give feedback.
To answer your question about passing the
user_id
to the pivot table while maintaining a select2 list of Pages, we can use therelationship
field type with some specific configurations. Here's how you can achieve this:user_id
in thewithPivot()
method: