open-admin-org / open-admin

open-admin forked from z-song/laravel-ladmin. Removing jquery, now based on Bootstrap5, vanilla JS
https://open-admin.org
MIT License
259 stars 75 forks source link

How to create list of child elements? #164

Open Demiurgich opened 8 months ago

Demiurgich commented 8 months ago

I have a list of users and sub-users.

Database:

$table->id();
$table->unsignedBigInteger('parent_id')->nullable();
$table->string('last_name');
$table->string('first_name');
$table->date('birthday')->nullable();
$table->string('phone')->nullable();
$table->string('email')->nullable();

Master-users have NULL parent_id. Sub-users have parent_id with id of master-user. There is only this two layers of users. In users grid I have a list of master-users.

Controller Grid:

protected function grid()
{
    $grid = new Grid(new Client());

    $grid->model()->whereNull('parent_id')->orderBy('last_name');

    $grid->column('Клієнт')->display( function() {
        return $this->last_name . ' ' . $this->first_name;
    } );
    $grid->column('phone', 'Телефон');
    $grid->column('email', 'Email')->hide();

    return $grid;
}

Controller Form:

protected function form()
{
    $form = new Form(new Client());

    $form->text('last_name', __('Прізвище'))->required();
    $form->text('first_name', __('Ім\'я'))->required();
    $form->date('birthday', __('День народження'));
    $form->phonenumber('phone', __('Телефон'));
    $form->email('email', __('Email'));

    $form->belongsTo('parent_id', ClientSelect::class, 'Контактна особа для');

    return $form;
}

How can I add to editing form of master-user a list of sub-users? This list should have links to edit those sub-users. How can I get the current user's data in Controller Form? I need to show some elements oft he form only if parent_id == null.