How to create a dependent select like country / city relationship using backpack-for-laravel?
Like: just show cities for the selected country
$this->crud->addField([ // Select2
'label' => "country",
'type' => 'select2',
'name' => 'country_id', // the db column for the foreign key
'entity' => 'country', // the method that defines the relationship in your Model
'attribute' => 'country', // foreign key attribute that is shown to user
'model' => "App\Models\Country" // foreign key model
]);
$this->crud->addField([ // Select2
'label' => "City",
'type' => 'select2',
'name' => 'city_id', // the db column for the foreign key
'entity' => 'city', // the method that defines the relationship in your Model
'attribute' => 'city', // foreign key attribute that is shown to user
'model' => "App\Models\City" // foreign key model
]);
There is a feature for that, but is not merged yet. You may take a look at [Feature][3.4][Ready] Select2_from_ajax can depend on any other input and check it out the modified files and reproduce in your installation.
Related
I am working on Laravel Backpack. I have two fields like this:
$this->crud->addField([ // SELECT2
'label' => 'Type',
'type' => 'select_from_array',
'name' => 'type',
'options' => [
'' => 'select type',
'Movie' => 'Movies',
'Series' => 'Series'
],
]);
$this->crud->addField([ // select2_from_ajax: 1-n relationship
'label' => "Entity", // Table column heading
'type' => 'select2_from_ajax',
'name' => 'entity_id', // the column that contains the ID of that connected entity;
'entity' => 'entity', // the method that defines the relationship in your Model
'attribute' => 'name', // foreign key attribute that is shown to user
'data_source' => url('api/entity'), // url to controller search function (with /{id} should return model)
'placeholder' => 'Select', // placeholder for the select
'include_all_form_fields' => true, //sends the other form fields along with the request so it can be filtered.
'minimum_input_length' => 0, // minimum characters to type before querying results
'dependencies' => ['type'], // when a dependency changes, this select2 is reset to null
// 'method' => 'GET', // optional - HTTP method to use for the AJAX call (GET, POST)
]);
The second field options are dependent on the first one.
In my model, I have:
public function getEntityIdAttribute()
{
$id = $this->attributes['entity_id'];
$type = $this->attributes['type'];
if ($type == "Movie") {
$attribute = Movie::find($id);
} else {
$attribute = Series::find($id);
}
return $attribute->name;
}
Create and List operations work perfectly. But on update, it throws this error:
Undefined array key "entity_id"
Why is this accessor not working on the update? or can we somehow skip the accessor on the update command?
At first, example tables:
Products:
id
name
price
Promo:
product_id (to Products id foreign)
old_price
new_price
What type of field i should use to get a price from foreign item selected before?
price value to old_price
Field where i select a product:
$this->crud->addField([
'label' => 'Product',
'type' => 'select',
'name' => 'product_id',
'entity' => 'products',
'attribute' => 'name',
//'model' => "Backpack\Products\app\Models\Products",
]);
Promos model function:
public function products()
{
return $this->belongsTo(Products::class,'product_id','id');
}
I tried to use a select2_ajax but i got a route problem (error 404).
As a promo has at least one product you can say type relationship with the exact namespace of your model.
$this->crud->addField([
'label' => 'Product',
'type' => 'relationship',
'name' => 'product_id',
'entity' => 'products',
'attribute' => 'name',
'model' => 'Backpack\Products\app\Models\Products',
]);
I have a Problem with relations in Laravel / with Backpack for Laravel.
This program is for creating menues for my dad´s restaurant.
I have these tables:
- dishes (Table with the Names of the Dishes)
-- id (auto inc)
-- menuname (Name of the Dish)
- weeklymenues
-- id (auto inc)
-- start_date (Monday of the selected week)
-- end_date (Friday of the selected week)
-- menu_monday (There should be the id of the dish)
-- menu_tuesday (...)
-- menu_wednesday (...)
.....
How can i do that correctly?
In the CRUD Controller i am setting the Field:
$this->crud->addField([
'label' => "Monday",
'type' => 'select2',
'name' => 'menu_mondy', // the db column for the foreign key
'entity' => 'menu', // the method that defines the relationship in your Model
'attribute' => 'menuname', // foreign key attribute that is shown to user
'model' => "App\Models\Menu" // foreign key model
]);
And in the menues model i have set this relation:
public function menu() {
return $this->belongsTo('\App\Models\Menu');
}
Everytime I want to save the CRUD, the program wants to save something in the dishes table:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'dish_id' in 'field list' (SQL: update `weeklymenues` set `dish_id` = 1, `weeklymenues`.`updated_at` = 2019-06-25 14:13:14 where `id` = 15)
What am I doing wrong? How can I set the relations correct?
Thanks in advance!!
Are "dishes" (from the table definition) and "Menu" (from the code) actually the same thing?
In your field definition, you set the model to be the "menu" class, shouldn't it be the "dishes" class?
I would have used the relationship type
CRUD::addField([
'label' => 'Monday',
'type' => 'relationship',
'name' => 'menu', // the method that defines the relationship in your Model
'attribute' => 'menuname', // foreign key attribute that is shown to user
'model' => \App\Models\dishes::class, // foreign key model
'placeholder' => 'Select a dish for Monday'
]);
---
-c:\xampp\htdocs\bpwebsite\app\Models\weeklymenues.php
---
protected $fillable = ['start_date', 'end_date', 'menu_monday']
---
I'm trying to set more than one attribute in a select2 type field.
For Example, I would like to show first_name and last_name as the label of my select values:
$this->crud->addField([ // Select
'label' => 'Employer',
'type' => 'select2',
'name' => 'employer_id', // the db column for the foreign key
'entity' => 'employer', // the method that defines the relationship in your Model
'attribute' => 'first_name', // foreign key attribute that is shown to user
'model' => 'App\Models\Employer' // foreign key model
], 'update/create/both');
Is there any suggestion?
Thanks.
You can with a getter:
public function getIdentNameAttribute()
{
return "{$this->ident} {$this->name}";
}
Then in your controller use:
"attribute" => "IdentName", // foreign key attribute that is shown to user
-- Typo3 CMS version 6.2 --
I have some entities which have a country property.
In the backend, when inserting/editing one of such entities I would like to be able to pick the country from a select.
As different entity types all share the same countries I thought it would be appropriate to store the countries in a db table. Then the countries are not going to be modified via the backend, so I don't need an admin interface for them.
Can this be achieved? How?
Up until now this is what I've come up with in my TCA configuration for one of my entities...
'country' => array(
'exclude' => 1,
'label' => 'LLL:EXT:ape/Resources/Private/Language/locallang_db.xlf:tx_myext_domain_model_building.country',
'config' => array(
'type' => 'select',
'foreign_table' => 'tx_myext_countries',
'size' => 1,
'maxitems' => 1,
'eval' => 'required'
),
)
...and this is the countries table sql...
CREATE TABLE tx_myext_countries (
uid int(11) NOT NULL auto_increment,
pid int(11),
name varchar(30) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
code varchar(3) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
PRIMARY KEY (uid)
);
...but while not giving any error it displays an empty select in the backend.
What am I missing?
Should I create a model for the countries? Should I treat them as entities? Would it be appropriate to configure it as an external data source?
Any help greatly appreciated!
Update
This is my attempt at configuring the tx_myext_countries table in TCA:
$GLOBALS['TCA']['tx_myext_countries'] = array(
'ctrl' => array(
'title' => 'Country',
'label' => 'name',
'searchFields' => 'name,code,',
),
);