I-am in the process of making this project where I have two (or more) fields having the same pivot table and am using the relationship type on backpack-for-laravel. What i want to do is have these two fields work independently.
So let's say I want to have one field where the user selects thickness while in the other field the user can select shading suppose.
The issue is that these two fields have the same pivot table and the same relationship set up in the model and therefore only one of them would show up in the same form. I do know that HTML forms replace the fields with the same "name" attribute but I'd like to know how can i get around solving this issue and have the fields work independently maintaining the select2_multiple functionality.
Table structure (Collection has n-n relationship with Collection Attributes)
Collection:
id
collection_name
.....
CollectionAttributes:
id
attribute_name
attribute_value
...
CollectionAttrValues
fk_col_id
fk_attr_val_id
Model relationship method:
public function collectionAttributeValues()
{
return $this->belongsToMany('App\Models\AttributeValue', 'collections_attr', 'fk_collection_id', 'fk_attr_val_id');
}
Controller fields code:
CRUD::addField([ // relationship
'type' => "relationship",
'name' => 'collectionAttributeValues', // the method on your model that defines the relationship
// OPTIONALS:
'label' => "Thickness",
'attribute' => "attr_val", // foreign key attribute that is shown to user (identifiable attribute)
'entity' => 'collectionAttributeValues', // the method that defines the relationship in your Model
'model' => "App\Models\AttributeValue", // foreign key Eloquent model
// 'placeholder' => "Select a category", // placeholder for the select2 input
'options' => (function ($query) {
$thickness_id = \App\Models\Attribute::select('attr_id')
->where('fk_attr_category', 1)
->where('attr_type', 'collection')
->where('attr_name', 'Thickness')->get()->first()->attr_id;
return $query->orderBy('attr_val', 'desc')->where('fk_attr_id', $thickness_id)->get();
}),
]);
CRUD::addField([ // relationship
'type' => "relationship",
'name' => 'collectionAttributeValues', // the method on your model that defines the relationship
// OPTIONALS:
'label' => "Shading",
'attribute' => "attr_val", // foreign key attribute that is shown to user (identifiable attribute)
'entity' => 'collectionAttributeValues', // the method that defines the relationship in your Model
'model' => "App\Models\AttributeValue", // foreign key Eloquent model
// 'placeholder' => "Select a category", // placeholder for the select2 input
'options' => (function ($query) {
$shading = \App\Models\Attribute::select('attr_id')
->where('fk_attr_category', 1)
->where('attr_type', 'collection')
->where('attr_name', 'Shading')->get()->first()->attr_id;
return $query->orderBy('attr_val', 'desc')->where('fk_attr_id', $shading)->get();
}),
]);
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.