multiple attributes in select2 field type - laravel-backpack

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

Related

Laravel Backpack attribute accessor causing bug on update command

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?

How to setup these relations correct in Backpack for Laravel

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']
---

how to create dependent select using backpack-for-laravel?

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.

How to set default value on Symfony3 EntityType?

I'm trying to set a default value on EntityType select like this:
$builder
->add('town', EntityType::class, array(
'label' => "TOWN",
'class' => 'AppBundle:Town',
'choice_label' => 'name',
'choice_value' => 'id',
'attr' => array('class' => 'form-control'),
'placeholder' => 'CHOOSE_AN_OPTION',
'required' => false,
'data' => $this->em->getReference('AppBundle:Town', $options['attr']['town'])
));
I was trying to do it using data option. It works fot a ChoiceType, but not for EntityType. I tried to pass to the data option the town entity ID, Name and the entire object.
I create the form object from the controller:
$finder = $this->createForm(UserFinderType::class, null,
array('attr' => array('role' => $this->getUser()->getRole(),
'town' => empty($params['town']) ? null : $townService->findOneById($params['town'])->getId() )));
When I pass Town ID or Name nothing happens, placeholder is shown. If I remove placeholder, blank option is selected.
When I pass entire Town object, this error is returned: Catchable Fatal Error: Object of class AppBundle\Entity\Town could not be converted to string
And, if I implement a __toString method, I get the same result that I get on ID or Name try.
You have to create __toString function in Town class:
AppBundle\Entity\Town
.................
public function __toString(){
return $this->name; // If you have `name` field or u can return `id`
}

Cakephp automatically filled form select for two word named belongTo model

What is right naming or what am I missing to get automagic run for two word named Model. Actual model belong to the two words named model.
Exact example:
Tour belongs to Accommodation type.
in database there is table tours and table accommodation_types
foreign key from tours is tours.accommodation_type_id
Snapshots of code below.
ToursController.php
public function add() {
//...
$accommodation_types = $this->Tour->AccommodationType->find('list');
//...
$this->set(compact('accommodation_types', ...));
}
Tour.php
//...
public $belongsTo = array(
//...
'AccommodationType' => array(
'className' => 'AccommodationType',
'foreignKey' => 'accommodation_type_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
//...
);
Tours/add.ctp (inside a form)
echo $this->Form->input('accommodation_type_id', array('label' => 'Accommodation type'));
As per convention the view vars names should be camelBacked. So rename the view var from $accommodation_types to $accommodationTypes. If you don't follow convention you have to explicitly specify the options var to use like this:
echo $this->Form->input('accommodation_type_id', array('options' => $accommodation_types, 'label' => 'Accommodation type'));