'SelectInput' get both selected ID and NAME key value and assign to separate key - admin-on-rest

I am using admin on rest create form. I have field to select 'category'.
<SelectInput source="category" choices={[
{ id: '1', name: 'Programming' },
{ id: '2', name: 'Lifestyle' },
{ id: '3', name: 'Photography' },
]} />
Here, When select 'category' I knew that 'id' value will be assigned to source 'category' key. But I wanted When select 'category' that 'name' value also must be assigned in 'category_name' key separately. So finally I wanted to pass both selected 'category' and 'category_name' also. How to achieve this?
Please someone help me.

I suggest you create a custom input for that: https://marmelab.com/admin-on-rest/Inputs.html#writing-your-own-input-component

Related

After an ajax need to load a field value and create a select_from_array with the range value

Iam trying and not finding a way to load the value from a field and generate a select_from_array based on its range.
For example:
I have 2 select box
Brand -> loads -> Model (using backpack field types, and its working good)
`'type' => 'select2_from_ajax',
'name' => 'camera_model_id',
'entity' => 'camera_model',
'attribute' => 'name',
'data_source' => url('camera-brands'),
'placeholder' => 'Selecione o Modelo',
'minimum_input_length' => 0,
'dependencies' => ['camera_brand_id'],`
But, after the user selects this last selectBox, I need that another field was modified
`'name' => 'channel',
'label' => "Canal da Câmera",
'type' => 'select2_from_array',
'options' => ['' => '',
'01' => '01',
'02' => '02', ...`
So, the options could be filled with the maximum of the field I registered in the Model field database.
Is it possible? or maybe another approach to achieve the solution?
Thanks in advance!
To have an input that depends on the value of another input, you can make both your fields select2_from_ajax.
That way:
you will have the value of all inputs in the controller (the controller that returns the ajax results; then you can return a filtered set of results depending on how the form is filled so far - CategoryController::index() in the documentation example);
you can use the "dependencies" attribute on the selec2_from_ajax fields, so that when one field is reset, both are;
I hope the answer helps someone. Cheers!

How to display products belonging to one user backpack laravel?

In my VendorsCrudController i make relation 1-n:
```
$this->crud->setModel('App\User');
$this->crud->addField([ // Address
'name' => 'vendor',
'type' => 'select',
'entity' => 'vendor', // the method that defines the relationship in your Model
'attribute' => 'name',
'model' => 'App\Dispensary',
]);
```
In User modal have method:
```
public function dispensaries() {
return $this->hasMany('App\Dispensary');
}
In Dispensary model:
public function vendor() {
return $this->belongsTo('App\User', 'user_id');
}
```
Table structure:
(users)
id: 1, name: Someone
id: 2, name: Someone2
(dispensaries)
id: 1, user_id: 1, name: SomeDispensary
id: 2, user_id: 2, name: SomeDispensary
How to display products belonging to one user. Now I get all the products for one user
You have multiple ways of filtering a CRUD table:
you can force the results to always be filtered using addClause (take a look here under ADVANCED QUERIES) ; this method is useful, for example, to only let a user see his own items (ex: $this->crud->addClause('where', 'user_id', '=', backpack_user()->id););
you can insert a filter above the CRUD table to help your admin filter the results however he likes (take a look at filters);
Hope it helps.

Laravel relationship with method "with" returns null

Today I wanted to do some clean code so just started selecting columns for with relationship. With this code:
\App\Genre::with([
'family'
])->where([
'slug' => $slug,
'is_active' => true
])->first();
everything is working fine. But when I start selecting columns for "with" method:
\App\Genre::with([
'family' => function ($query) {
$query->select('name_pl', 'name_lat');
}])->where([
'slug' => $slug,
'is_active' => true
])->first();
I got that family is null (but it should be an object with columns: name_pl, name_lat). What I am doing wrong?
family method in Genre class looks like this:
public function family () {
return $this->belongsTo(Family::class);
}
I am using Laravel 5.4
Pretty sure you need to add a related column to the list of selected columns, otherwise Laravel won't b able to match the data to eager-load.
Assuming that Genre has a family_id and Family has an id primary key column specified, you need this:
$query->select('id', 'name_pl', 'name_lat'); // See the id added here?
Should do the trick.
For clarity, the matching I mentioned is this one:
select * from genre
select * from family where id in (1, 2, 3, 4, 5, ...)
-- where the comma-separated list of IDs consists of the unique family_id values retrieved in the first query.
Why don't you try:
\App\Genre::with('family:name_pl,name_lat')->where([
'slug' => $slug,
'is_active' => true
])->first();

How to add multi select value in database in October CMS Backend?

Not inserted multi select value in database.
My fields.yaml Code is :
related_recipe:
label: 'Related Recipe'
span: auto
nameFrom: recipe_title
descriptionFrom: description
attributes: {multiple:'multiple'}
type: relation
My model code is :
public $belongsTo = [
'related_recipe' => [
'Qdata\Taeq\Models\Recipe',
'conditions' => 'status = 1'
],
];
Currently only one selected value inserted in database.need add multiple value in database. Can any one have the solution of it ?
You should use a $belongsToMany relation in this case.
$belongsTo means your model is link with a single entity of your Recipe model.
To do so with relation you need to go with "belongsToMany" relation. for example:
In your Model
'related_recipes' => [
'Qdata\Taeq\Models\Recipe',
'table' => 'pivot_table_name',
'key' => 'foreign_key_of_pivot_table',
'otherKey' => 'other_key',
],
In related another Model
'makers' => [
'Qdata\Taeq\Models\AnotherModel',
'table' => 'pivot_table_name',
'key' => 'foreign_key_of_pivot_table',
'otherKey' => 'other_key',
],
this will save your multiselect dropdown data in the related pivot table in your database.

Grocery crud - How to change fields values on view/read

Can someone help me please ?
if ($crud->columns['NAME'] == '')
{
$crud->columns['NAME'] = "Anonymous";
}
How can I do it the right way ?
I think you need to give more information about your case, for example, field type.
From Grocery Crud documentation:
http://www.grocerycrud.com/documentation/options_functions/field_type#dropdown-field
If you have a dropdown field named 'status', you can set values like this:
$crud->field_type('status','dropdown',
array('1' => 'active', '2' => 'private','3' => 'spam' , '4' => 'deleted'));
You will have the values set for view/read and edit.

Resources