How to add default value in drodown in october cms? - laravel-5

On Fields.yaml file code is :
category:
label: Category
nameFrom: category_name
descriptionFrom: description
span: auto
type: relation
My controller code is :
public $belongsTo = [
'category' => [
'Ashish\Taeq\Models\RecipeCategory',
'conditions' => 'status = 1'
],
I need to set default category drop-down like "Please select category".

Please Use
emptyOption: " "
on On Fields.yaml
category:
label: Category
nameFrom: category_name
descriptionFrom: description
span: auto
type: relation
emptyOption: " "
Or Please follow below link : https://octobercms.com/forum/post/filtering-form-fields-with-dependson-problem

Related

Laravel Backpack select2_from_ajax select change the result and default value

For the backpack-demo, I would like to show the ID and Title together.
For the search result no issue, selected at the select2 also no issue, the problem is after save, it will not display, it become blank.
How can I set it?
Like below
backpack-demo/app/Http/Controllers/Admin/MonsterCrudController.php
[ // select2_from_ajax: 1-n relationship
'label' => 'Select2_from_ajax', // Table column heading
'type' => 'select2_from_ajax',
'name' => 'select2_from_ajax', // the column that contains the ID of that connected entity;
'entity' => 'article', // the method that defines the relationship in your Model
'attribute' => 'id_title', // foreign key attribute that is shown to user
'model' => "Backpack\NewsCRUD\app\Models\Article", // foreign key model
'data_source' => url('api/article'), // url to controller search function (with /{id} should return model)
'placeholder' => 'Select an article', // placeholder for the select
'minimum_input_length' => 2, // minimum characters to type before querying results
'tab' => 'Relationships',
'wrapperAttributes' => ['class' => 'form-group col-md-12'],
],
backpack-demo/app/Http/Controllers/Api/ArticleController.php
if ($search_term) {
return Article::
selectRaw("CONCAT('Article ID: ', id,' | Title: ', Title) AS id_title, articles.*")
->where('title', 'LIKE', '%'.$search_term.'%')->paginate(10);
} else {
return Article::selectRaw("CONCAT('Article ID: ', id,' | Title: ', Title) AS id_title, articles.*")
->paginate(10);
}
The solution is at the Article Model create another accessor function
public function getIdTitleAttribute()
{
return 'Article ID: ' . $this->id . ' | Title: ' . $this->title;
}

YII Select2 Dropdown

I have a code where I want to display the countries data along with the country code. The dropdown must contain both the name and the code. I want that whenever a user selects a country the select bar must display only the code, not the name. for example if a user is from Canada the select bar should display +1. I am using select2 widget in yii2 for dropdown; Here is my code:
$countriesIsdData = ArrayHelper::map($countries_isd_codes, 'countries_isd_code','countries_name');
echo $form->field($model,'country_code')->widget(Select2::classname(),
[
'data' => $countriesIsdData,
'value'=>$count,
'options' => [
'id'=>'country_cod',
'placeholder'=>'Select Country Code',
'prompt' => 'Select Country Code','value'=>$count
],
'pluginOptions' => [
'allowClear' => true,
'dropdownCssClass'=>'bigdrop',
'value'=>$count
]
])->label('Country Code');

'SelectInput' get both selected ID and NAME key value and assign to separate key

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

OctoberCMS Rainlab.Builder- Error on "hasOne" relation to same model

I'm developing an October CMS Plugin for managing animals (with rainlab.builder).
Animals have a couple of fields and relations. Every animal have a father and a mother animal. But when I try to safe my animal the following error appears:
Event Log:
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048
Column 'id' cannot be null in dir/website/vendor/laravel/framework/src/
Illuminate/Database/Connection.php:413
Plugin animal form:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'id'
cannot be null (SQL: update `prefix_animals_animal` set `id` = where
`prefix_animals_animal`.`id` = 1 and `prefix_animals_animal`.`id` is
not null)" on line 666 of dir/website/vendor/laravel/framework/
src/Illuminate/Database/Connection.php
The error only appers when I use the relations (child -> father, child -> mother).
Code
I have implemented the following hasOne - relations to my animal model:
/* Relation */
public $hasOne = [
'father' => [
'Namespace\Animals\Models\Animal',
'key' => 'id',
'otherKey' => 'id'
],
'mother' => [
'Namespace\Animals\Models\Animal',
'key' => 'id',
'otherKey' => 'id'
]
];
This are my fields from field.yaml:
father:
label: Father
oc.commentPosition: ''
nameFrom: name
descriptionFrom: description
emptyOption: 'No father'
span: left
type: relation
mother:
label: Mother
span: right
oc.commentPosition: ''
nameFrom: name
descriptionFrom: description
emptyOption: 'No mother'
type: relation
I would be very happy if someone have a solution for these kind of relations.
Cheerio!
Don't use id as the primary key for the relation as this is not good practice.
Actually this creates the problem you are seeing also. Currently your model is using the id for primary Id, mother Id and father Id. You can't do that.
Add mother_id and father_id to the Animal model and change the relation definition to this:
public $hasOne = [
'father' => [
'Namespace\Animals\Models\Animal',
'key' => 'father_id',
'otherKey' => 'id'
],
'mother' => [
'Namespace\Animals\Models\Animal',
'key' => 'mother_id',
'otherKey' => 'id'
]
];
PS. In your case you dont have to define key and otherKey as the default value for otherKey is "id" and the otherKey value is created from the relation name with the "_id" suffix.

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.

Resources