Skip to content
Roberts Jurkjans edited this page Mar 10, 2018 · 5 revisions

Arbory modules allows you to easily create a CRUD (Create, Read, Update, Delete) interface for your Eloquent Model.

Create

To create a new Arbory module, you need to define a new controller which uses Crudify trait and register which Eloquent model will be used

class UsersController extends Controller
{
    use Crudify;

    protected $resource = User::class;
}

Registration

When controller is created, you need to register it in routes/admin.php.

Admin::modules()->register( App\Http\Controllers\Admin\UsersController::class );

Arbory will automatically generate module slug and register resource routes - index, show,create,store,edit,update,destroy.

Module slug is generated from controller full class name by throwing out unneeded parts and using str_slug method to make it URI friendly. In this example index route URI will be /admin/text/, but for controller App\Http\Controllers\Admin\Product\TagsController::class it will be /admin/product-tags/

Get module URLs

$module->url('index');
$module->url('activate', ['id' => $userId]);

API and Dialog routes

In module registration process two magic routes api and dialog are registered. These routes allows you to simply access some API or render dialog of module. For example, to access user's API you need to call URI /admin/users/api/list. Module automatically finds and call method listApi and returns the result. Same logic is used for dialogs. URL's can be made using $module->url('api', ['api' => 'list'])

Custom routes

If it's necessary, you can define custom routes for your module. Add closure as second parameter for register() method and you will get Illuminate Router instance, which allows you to define routes in this module context.

Admin::modules()->register(UsersController::class, function (Router $router) {
    $router->post('{id}/activate', UsersController::class . '@activate')->name('activate');
});

Grid

Definition

public function grid()
{
    return $this->module()->grid($this->resource(), function (Grid $grid) {
        $grid->column('first_name', 'First name');
        $grid->column('last_name');
        $grid->column('email');
    });
}

If column title isn't defined, name will be used as column title.

Sortable fields

$grid->column('first_name')->sortable();

Searchable fields

All grid fields by default are searchable. To disable search for field, you can add searchable(false)

$grid->column('first_name')->searchable(false);

Customize column output

$grid->column('email', 'Avatar')->display(function ($value) {
    return '<img src="//www.gravatar.com/avatar/' . md5($value) . '?d=retro" width="32" alt="' . $value . '">';
});

Relations

You can also output object relation fields. All relations will be loaded with eager load.

$grid->column('phone.number');

Custom query

By default Arbory loads all resources, but if you need custom query to filter out some records, you can use filter() method.

$grid->filter(function (Grid\Filter $filter) {
    $filter->getQuery()->where([...]);
});

Form

Form structure is defined in form() method. Form is used for create and edit views.

protected function form(Model $model)
{
    return $this->module()->form($model, function (Form $form) {
        $form->addField(new Text('first_name'));
        $form->addField(new Text('last_name'));
    });
}

To add field to form addField() method must be used. Field instances must be created with model attribute names.

$form->addField(new Text('first_name'));

In edit view field value will be filled automatically.

Possible field types

  • Text - text input
  • Textarea - simple HTML textarea
  • Richtext - WYSIWYG editor. By Default CK Editor
  • CompactRichtext - minimalistic WYSIWYG editor
  • Checkbox - standard HTML checkbox. Given value will be saved if checkbox will be checked
  • Boolean - standard HTML checkbox. 1 or 0 will be saved
  • Select - HTML select
  • DateTime - date picker
  • Password - HTML password field
  • MapCoordinates - Google MAP point
  • Slug - HTML input with integrated API to generate model slug
  • SpriteIcon - dropdown with predefined SVG icons
  • Hidden - HTML hidden field
  • ArboryFile - image upload field
  • ArboryImage - file upload field

Relation types

  • BelongsTo - outputs select with related model options
  • BelongsToMany - outputs checkbox list with relate model options
  • HasMany - outputs nested form with possibility to define multiple related items
  • HasOne - outputs nested form with possibility to define single related item
  • Link - predefined HasOne relation which consists from URL, title and target fields
  • ObjectRelation - possibility to link single or multiple Eloquent models to current model
  • MultipleSelect - HTML select with possibility to select multiple items
  • Translatable - used for i18n fields

Nested forms are defined using field sets

$form->addField(new HasMany('attachments', function (FieldSet $fieldSet) {
    $fieldSet->add(new Text('name'));
    $fieldSet->add(new ArboryFile('file'));
}));

Validation

Validation can be organised using Laravel Form Requests or by defining rules for form field

$fieldSet->add(new Text('name'))->rules('required')

Arbory uses Laravel Validator so rules also are defined the same way.

Hooks

It's posible to hook some actions before or after arbory actions

  • validate.before
  • create.before
  • create.after
  • update.before
  • update.after
  • delete.before
  • delete.after