Skip to content

Commit 9856932

Browse files
committed
[EH] add full readme
1 parent 6280699 commit 9856932

File tree

1 file changed

+119
-1
lines changed

1 file changed

+119
-1
lines changed

README.md

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,130 @@ composer require webfactor/laravel-backpack-instant-fields
2020

2121
## Usage
2222

23+
### EntityCrudController
24+
25+
In the EntityCrudController that is supposed to PROVIDE instant creation (not in the CrudController where you want to USE instant fields!) you have to embed the `CanBeCreatedOnTheFly` trait from this package.
26+
27+
```php
28+
<?php
29+
30+
use Webfactor\Laravel\Backpack\InstantFields\CanBeCreatedOnTheFly;
31+
32+
class EntityCrudController extends CrudController
33+
{
34+
use CanBeCreatedOnTheFly;
35+
36+
//
37+
}
38+
```
39+
40+
This trait provides all needed route entry points methods and ajax response methods.
41+
42+
### Routes
43+
44+
in your routes file you have to add three additional routes for you `CRUD::resource`. For clarity we recommend to use the `with()` helper:
45+
46+
```php
47+
<?php
48+
49+
CRUD::resource('entity', 'EntityCrudController')->with(function () {
50+
Route::get('entity/ajax/create', 'EntityCrudController@ajaxCreate');
51+
Route::get('entity/ajax', 'EntityCrudController@ajaxIndex');
52+
Route::post('entity/ajax', 'EntityCrudController@ajaxStore');
53+
});
54+
```
55+
56+
### Available Fields
57+
58+
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:
59+
60+
- [select2_from_ajax](https://laravel-backpack.readme.io/docs/crud-fields#section-select2_from_ajax)
61+
- [select2_from_ajax_multiple](https://laravel-backpack.readme.io/docs/crud-fields#section-select2_from_ajax_multiple)
62+
63+
To use instant creation capability of these field types you have to add the `on-the-fly` key
64+
65+
```
66+
'on_the_fly' => [
67+
'create_view' => backpack_url('entity/ajax/create'),
68+
]
69+
```
70+
71+
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.
72+
2373
Example:
2474

75+
```
76+
[
77+
'name' => 'entity_id',
78+
'type' => 'select2_from_ajax',
79+
'label' => 'Entity',
80+
'view_namespace' => 'webfactor::fields',
81+
'model' => Entity::class,
82+
'entity' => 'entity',
83+
'attribute' => 'name',
84+
'data_source' => backpack_url('entity/ajax'),
85+
'placeholder' => 'Choose',
86+
'minimum_input_length' => 0,
87+
'on_the_fly' => [
88+
'create_view' => backpack_url('entity/ajax/create'),
89+
],
90+
],
91+
```
92+
93+
## Multiple instant fields
94+
95+
If you want to use more than one instant field in a CrudController you have to define separate names for each so that JQuery is able to trigger the modals in the right way.
96+
97+
### EntityCrudController
98+
99+
In the EntityCrudController that provides instant creation you have to set the `$ajaxEntity` property by using the setter in the `setup()`-method:
100+
25101
```php
26102
<?php
27103

28-
// to be added
104+
use Webfactor\Laravel\Backpack\InstantFields\CanBeCreatedOnTheFly;
105+
106+
class EntityCrudController extends CrudController
107+
{
108+
use CanBeCreatedOnTheFly;
109+
110+
public function setup()
111+
{
112+
// other Backpack options
113+
$this->setAjaxEntity('name_of_entity');
114+
115+
// fields/columns definitions
116+
}
117+
}
118+
```
119+
120+
### Field definition
121+
122+
In the field definition you have to add `entity` to the `on-the-fly` key and give it the exact same name as in the EntityCrudController above.
123+
124+
```
125+
'on_the_fly' => [
126+
'create_view' => backpack_url('entity/ajax/create'),
127+
'entity' => 'name_of_entity',
128+
]
129+
```
130+
131+
## Customization
132+
133+
### Search behavior
134+
135+
If you need a different search behavior just overwrite the `ajaxIndex()` method in your `EntityCrudController` and write your own search logic.
136+
137+
### Request validation
138+
139+
You can also use Request Validation! Just copy the `ajaxStore()` method to your `EntityCrudController` and replace `Request` by your desired request (usually just `StoreRequest`).
140+
141+
### Fields
142+
143+
Publish the fields in your project and modify functionality
144+
145+
```
146+
php artisan vendor:publish --tag=instantfields
29147
```
30148

31149
## Change log

0 commit comments

Comments
 (0)