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

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.

Related

save both field same name relationship one to one backpack laravel

I have two tables with relationship one to one hasOne and have column same name
CRUD::addField([
'label' => "Title",
'type' => 'text',
'name' => 'new_title', // the db column for the foreign key
]);
CRUD::addField([
'label' => "Title",
'type' => 'text',
'name' => 'achive.new_title', // the db column for the foreign key
'entity' => 'achive',
]);
I just want show once but save both to two table
You need something that's call the callbacks method, the simplicity of that method is you can control what you want after create, edit, delete and etc.
So, in your controller just need one field name,
CRUD::addField([
'label' => "Title",
'type' => 'text',
'name' => 'new_title',
]);
After that, you can create a new trait in your controller for creating/updating your relationship,
use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation { store as traitStore; }
And then, declare the store function,
public function store() {
// Get the value from the request
$request = $this->crud->getRequest();
// And then you can insert/update data into your relationship table
\DB::table('achives') // Your relation table
->updateOrInsert(
['new_title' => $request->new_title],
['other_column_name' => 'Value of your other column']
);
$response = $this->traitStore();
// do something after save
return $response;
}
The updateOrInsert method accepts two arguments: an array of conditions by which to find the record, and an array of column and value pairs indicating the columns to be updated. more info here

Laravel backpack addField where clause

Using this link.
I tried to add a where clause in addField where the dropdown should load the contents as options only if they match a condition.
Here is the code:
$this->crud->addField([ // select_from_array
'name' => 'manager',
'label' => "Manager Name",
'type' => 'select_callback', // Custom field type of select2
'entity' => 'Manager',
'attribute' => 'name',
'model' => 'App\Models\Manager',
'scope' => 'manager'
// 'allows_multiple' => true, // OPTIONAL; needs you to cast this to array in your model;
], 'update/create/both');
And in Model.php
public function scopeManager($query)
{
return $query->where('gym_code', Auth::user()->gym_code);
}
But it is not working!!
Thanks
I found the answer in the same link that I suggested in Question.
In the link #thplat answered a change in select_callback.blade.php
For the select_callback view I took the select view and changed only one line there.
We go from:
#foreach ($field['model']::all() as $connected_entity_entry) to #foreach ((isset($field['callback']) ? $field['callback']() : $field['model']::all()) as $connected_entity_entry).

how to concatenate two columns in yii2 activerecord?

I am new to developing in yii2 (and yii). I have a query that returns name and last name of customers along with other columns. I am using activeDataProvider to display the records in a table but I need the name and lastname be displayed in one table column.
What I want to know is:
1. how do I concatenate the columns in yii2 activerecord?
2. How do I display the columns in one table columns made gridView?
below is what I have tried that none works:
1. $query->select(['customer.*', 'fullname'=>concat('customer.name'," ",'customer.lastname')]);
2. 'dataProvider' => $model['dataProvider'],
'columns' => [
'id',
'name'.' '.'lastname',
'customer_orders',
'created_at:datetime'
Use GridView like below.
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'id',
[
'attribute' => 'name',
'value' => function ($model) {
return $model->name . ' ' . $model->lastname;
}
]
'customer_orders',
'created_at:datetime'
]
]) ?>
Refer Yii2 GridView

Cake3 Paginator Sort Error

I have a very simply model which I want to paginate. The pagination works but the sort links do not have any effect:
My Controller:
public $paginate = [
'limit' => 10,
'order' => [
'Properties.id' => 'asc'
],
'sortWhitelist' => [
'Properties.id',
'Properties.name',
'Properties.active'
],
];
My query:
$properties = $this->Properties->find('all')->where($options)->contain($contains)->order(['Properties.id']);
$this->set('properties', $this->paginate($properties));
My view displays 10 items per page and the links to pages/next/previous work fine. When I click the sort link:
$this->Paginator->sort('id', 'ID')
The url called is:
properties/index/3?sort=id&direction=desc
The page re-loads but the order of the data does not change.
The whitelist expects the exact field name that you are using in sort. It will not automatically prepend the primary table's name, so your whitelist should look like this:
public $paginate = [
'limit' => 10,
'order' => [
'Properties.id' => 'asc'
],
'sortWhitelist' => [
'id',
'name',
'active'
],
];
It will see "id" in the query and check that the exact field name "id" exists in the whitelist.
Also, it appears you are including an order() on your query. To allow the paginator to set the order, you should remove this clause.

correct value instead of array

scenario is crm with tables account, account_contact, contact and account_contact_role. The latter contains roles like 'project lead' or 'account manager' for the combos defined in the junction table.
My challenge is the account view, that is listing also the connected persons with their roles. I want my grid to show: Doe | John | employee.
The problem is now when the contact has 2+ entries in the junction table. How can I print the correct role for the row? As you can see in the code I solved it the static way which shows only 1 out of n times the correct value. Tried it with inner join without success. Is it a problem of the search in the model or with the access in the view?
the relation from the account model to the contacts:
public function getContacts($role = null)
{
// many-to-many
return $this->hasMany(ContactRecord::className(), ['id' => 'contact_id'])
->via('accountContacts')
->innerJoinWith(['accountContacts.role'])
->andWhere(['account_contact.account_id' => $this->id])
->andWhere(['account_contact_role.type' => $role])
;
}
the view
<?= \yii\grid\GridView::widget([
'dataProvider' => new \yii\data\ActiveDataProvider([
'query' => $model->getContacts('internal'),
'pagination' => false
]),
'columns' => [
'lastname',
'firstname',
[
'label' => 'Role',
'attribute' => 'accountContacts.0.role.name',
],
[
'class' => \yii\grid\ActionColumn::className(),
'controller' => 'contacts',
'header' => Html::a('<i class="glyphicon glyphicon-plus"></i> Add New', ['contact-records/create', 'account_id' => $model->id]),
'template' => '{update}{delete}',
]
]
]); ?>
defined relations are:
account has many accountContacts has one contact
accountContacts has one accountContactRole
Many thanks in advance!
You are showing account's contacts, so you have to list from Contact model.
Inside Contact model (or Contact ActiveQuery file):
public static function queryContactsFromAccountAndRole($account, $role = null)
{
// many-to-many
return ContactRecord::find()->innerJoinWith(['accountContacts' => function($q) use($account, $role) {
$q->innerJoinWith(['accountContactsRole'])
->andWhere(['account_contact.account_id' => $account->id])
->andWhere(['account_contact_role.type' => $role]);
}])
->andWhere(['account_contact.account_id' => $account->id]);
}
Now you have one record for each contact and the gridview will show all contacts.

Resources