Download the PHP package webfactor/laravel-backpack-instant-fields without Composer
On this page you can find all versions of the php package webfactor/laravel-backpack-instant-fields. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download webfactor/laravel-backpack-instant-fields
More information about webfactor/laravel-backpack-instant-fields
Files in webfactor/laravel-backpack-instant-fields
Package laravel-backpack-instant-fields
Short Description Instant fields to create/edit/delete related entities on the fly in Laravel Backpack
License MIT
Homepage https://github.com/webfactor/laravel-backpack-instant-fields
Informations about the package laravel-backpack-instant-fields
laravel-backpack-instant-fields
This is a package for Laravel Backpack and provides CRUD field types which allow to create/edit/delete a related CRUD entity on-the-fly while adding/editing another.
Install
Via Composer
Usage
EntityCrudController
For simplicity add the InstantFields
trait from this package to all EntityCrudControllers which are supposed to provide "instant fields" or are triggered by "instant fields".
This trait provides all needed route entry points methods and ajax response methods.
Routes
in your routes file you have to add one additional route in your CRUD::resource
for each Entity that uses the packages trait. For clarity we recommend to use the with()
helper:
The trait/route will handle the following requests for you:
- search on triggered entity
- retrieve the HTML for the create/edit/delete modal
- store/update/destroy foreign entity
Available Fields
There are two field types available in this package which allow you an instant creation of related models (1-n and n-m). They are modified versions of the equivalent field types that already exist in Laravel Backpack:
Attention:
Edit and delete buttons are only available for
select2_from_ajax
Please consider your database constraints when using the delete button!
Field Definition
Please set the $ajaxEntity
property by using the setter in the setup()
-method of the (foreign) EntityCrudController that is triggered by an "instant field":
In the field definition of the EntityCrudController
where your instant field is setup you will have to set the above name in the on_the_fly
-Array.
Note: If you use Laravel Backpack Crud >=3.4.11 you don't have to publish the provided fields, you can use them directly from the package by using the
view_namespace
key.
Example:
Instant Fields will try to auto-fill the select2 input after creating a new entry. It will assume that an input field exists with the name name
and will use its value for the triggered ajax search. If you want to use another field for this, just add attribute
to the on_the_fly
-array containing the field name you want to use.
Sometimes you may need a simple Button/Link to the "real" foreign entity without a modal: Just add
'crud' => true
in that case
List view
With this package your are also able to add a create button for the foreign CRUD entity in your list view of Backpack! Just add the following line in your EntityCrudController
:
Customization
Modal view
By route
By default the modals are loaded automatically by using entity
in on_the_fly
of the field definition, e.g. resulting in backpack_url($field['on_the_fly']['entity']).'/ajax/create'
for the create modal.
You can overwrite this behavior for all modals by setting an attribute:
Please be aware that by using this attributes you will be completely responsible for the content of the modal! The defined request has to provide valid HTML which is then filled in
<div class="modal-content"></div>
By view
Instead of defining a route you can also use a custom view by:
Search logic
The "instant field" triggers the ajaxIndex()
of the EntityCrudController
where the field is defined and uses the fields model
and attribute
parameters to perform the search on the foreign model.
By adding search_logic
to the field defintion you can implement your own searching behavior:
Collecion $form
is an optional parameter and provides all current values of your CRUD form. You can use it to manipulate your search depending on actual inputs and in combination with dependencies
(see Backpack Documentation)
Furthermore you can then use attibute
to display enriched values in the dropdown by using an accessor on the model.
Search data source
If needed you are free to use the data_source
and method
attributes from the original field blades which come with Laravel Backpack. This is the URL that is triggered by the select2 field for searching.
Request validation
You can also use Request Validation! Just set the $ajaxStoreRequest
and/or $ajaxUpdateRequest
property by using the provided setter method:
Auto-fill after store/update
Instant Fields will try to auto-fill the select2 input after creating a new entry.
It will assume that an input field exists with the name name
and will use its value for the triggered ajax search.
You can add an autofill_attributes
array, if you want to define one or multiple auto-fill columns:
Please note that this only works with columns (or appended accessors).
In order for this to work properly on multiple columns, you should implement a custom search logic for the field.
Search logic example:
Passing current field values to foreign EntityCrudController
Sometimes you will need current values to be used for the creation of an foreign entity. You may define serialize
with the IDs of the fields you need in the store request:
So the current values of type_id
and name
will be available in the $request
of the ajaxStore
method of your foreign EntityCrudController
(you will have to overwrite it).
Fields
Publish the fields in your project and modify functionality
Change log
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CODE_OF_CONDUCT for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
- Thomas Swonke
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-backpack-instant-fields with dependencies
illuminate/database Version ~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0
php Version ~7.1