relations widgets for backpack
3.3.1

composer require izica/relations-widgets-for-backpack

relation_panel
name - name of relationlabel - panel labelbackpack_crud - backpack crud url,buttons (optional) - set false to hide all action buttonsbutton_show (optional) - set false to hidebutton_edit (optional) - set false to hidevisible (optional) - closure for hiding or showing panelfields (optional) - fields array, by default get columns from fillable in model
name - namelabel - for fieldclosure - use closure instead of name field,visible(optional) - closure for hiding or showing panelrelation_table
name - (required) name of relationlabel - panel labelrelation_attribute - (optional) used for passing url parameter name for button_createsearch - (optional) closure, enables search inputper_page - (optional) enables pagination, null by defaultbackpack_crud - backpack crud url,buttons (optional) - set false to hide all action buttonsbutton_create (optional) - set false to hidebutton_show (optional) - set false to hidebutton_edit (optional) - set false to hidebutton_delete (optional) - set false to hidevisible (optional) - closure for hiding or showing panelcolumns (optional) - columns array, by default get columns from fillable in model
name - namelabel - for fieldclosure - use closure instead of name field for passing value,belongsTo, hasOne
use BackpackCRUDappLibraryWidget;
protected function setupShowOperation()
{
Widget::add([
'type' => 'relation_panel',
'name' => 'account_contact',
'label' => 'Account contact info',
'backpack_crud' => 'accountcontact',
'visible' => function($entry){
return $entry->is_public_person;
},
'buttons' => false,
'fields' => [
[
'label' => 'Birthdate',
'closure' => function($entry){
return date('d.M.Y', $entry->birthdate);
}
],
[
'label' => 'Contact phone',
'name' => 'contact_phone',
],
[
'label' => 'Contact email',
'name' => 'contact_email',
],
[
'label' => 'Address',
'name' => 'address.name',
'visible' => function($entry){
return !!$entry->address;
}
],
],
])->to('after_content');
}hasMany
protected function setupShowOperation()
{
Widget::add([
'type' => 'relation_table',
'name' => 'order_cargos',
'label' => 'Order cargo list',
'backpack_crud' => 'ordercargo',
'visible' => function($entry){
return $entry->order_cargos->count() > 0;
},
'search' => function ($query, $search) {
return $query->where('name', 'like', "%{$search}%");
},
'relation_attribute' => 'order_id',
'button_create' => true,
'button_delete' => false,
'columns' => [
[
'label' => 'Type',
'name' => 'order_cargo_type.name',
],
[
'label' => 'Weight',
'name' => 'weight',
],
[
'label' => 'Value, $',
'closure' => function($entry){
return "{$entry->value}$";
}
],
],
])->to('after_content');
}You need to set:
button_create => truerelation_attribute => attribute_nameNext you need to add to relation/select field default value:
CRUD::addField([
'type' => "relationship",
'name' => 'order',
'default' => $_GET['order_id'] ?? null
]);