Laravel-admin: Multiselect :: How to?

Created on 25 Mar 2017  ·  7Comments  ·  Source: z-song/laravel-admin

Hi!

I would like to use multiselect:
A => B => C
"Select A options" is a static list
"Select B options" depends on "Select A Value"
"Select C options" depends on "Select B Value"

How is it possible?

Thank you!

Most helpful comment

I have manged to gain multiselect feature. Rooting is ok now too.
API Routing for me:

Route::group([
  'prefix' => config('admin.prefix'),
  'namespace' => Admin::controllerNamespace(),
  'middleware' => ['web', 'admin'],
], function (Router $router) {
  $router->get('/', 'HomeController@index');
  $router->resource('regions', RegionController::class);
  $router->resource('places', PlaceController::class);
  $router->resource('room_types', RoomTypeController::class);
  $router->resource('rooms', RoomController::class);

  Route::group([
    'prefix' => 'api'
  ], function (Router $router) {
    $router->get('room-type-by-place-id', 'ApiController@roomTypeByPlaceId');
  });
});

Then the Form looks like this:

      $form->select('place_id')->options(
        Place::all()->pluck('name', 'id')
      )->load('room_type_id', '/admin/api/room-type-by-place-id');
      $form->ignore('place_id');
      $form->select('room_type_id')->options(function ($id) {
        return RoomType::options($id);
      })->rules('required');

Also important to set "brothership" relation in used Models:

<?php

namespace App;

class RoomType extends ModelSingular
{
  public function rooms()
  {
    return $this->hasMany(Room::class, 'room_type_id');
  }

  public function parent()
  {
    return $this->belongsTo(Place::class, 'place_id');
  }

  public function brothers()
  {
    return $this->parent->roomTypes();
  }

  public static function options($id)
  {
    if (!$self = static::find($id)) {
      return [];
    }

    return $self->brothers()->pluck('name', 'id');
  }
}

All 7 comments

Ref to http://120.26.143.106/admin/demo/users/18/edit#tab-form-4

With following code:

$form->select('address.province_id')->options(
    ChinaArea::province()->pluck('name', 'id')
)->load('address.city_id', '/admin/api/china-area/city');

$form->select('address.city_id')->options(function ($id) {
    return ChinaArea::options($id);
})->load('address.district_id', '/admin/api/china-area/district');

$form->select('address.district_id')->options(function ($id) {
    return ChinaArea::options($id);
});

I like your demo. Thank you for point on the current code reference.
Does your demo project available on github?
I want to check api routes etc.

By the way: not the best practice to insert html or js script fragments in php code.
For example: src/Form/Field/Select.php:87
You should call js fregments from external js file and views too.

I have manged to gain multiselect feature. Rooting is ok now too.
API Routing for me:

Route::group([
  'prefix' => config('admin.prefix'),
  'namespace' => Admin::controllerNamespace(),
  'middleware' => ['web', 'admin'],
], function (Router $router) {
  $router->get('/', 'HomeController@index');
  $router->resource('regions', RegionController::class);
  $router->resource('places', PlaceController::class);
  $router->resource('room_types', RoomTypeController::class);
  $router->resource('rooms', RoomController::class);

  Route::group([
    'prefix' => 'api'
  ], function (Router $router) {
    $router->get('room-type-by-place-id', 'ApiController@roomTypeByPlaceId');
  });
});

Then the Form looks like this:

      $form->select('place_id')->options(
        Place::all()->pluck('name', 'id')
      )->load('room_type_id', '/admin/api/room-type-by-place-id');
      $form->ignore('place_id');
      $form->select('room_type_id')->options(function ($id) {
        return RoomType::options($id);
      })->rules('required');

Also important to set "brothership" relation in used Models:

<?php

namespace App;

class RoomType extends ModelSingular
{
  public function rooms()
  {
    return $this->hasMany(Room::class, 'room_type_id');
  }

  public function parent()
  {
    return $this->belongsTo(Place::class, 'place_id');
  }

  public function brothers()
  {
    return $this->parent->roomTypes();
  }

  public static function options($id)
  {
    if (!$self = static::find($id)) {
      return [];
    }

    return $self->brothers()->pluck('name', 'id');
  }
}

How do I return the price of a product on a product select. No select only returns the product_id. How do I fill the price field from select?

If I have 3 selects that depend on each one, how I can pass more data via load() ajax for select2?

I have this:

        $form->select('categories')->options(
            Category::all()->pluck('name', 'id')
        )->load('categories2', '/admin/api/categories/secondary');

        $form->select('categories2')->options(function ($id) {
            return Category::options($id);
        })->load('categories3', '/admin/api/categories/tertiary');

        $form->select('categories3')->options(function ($id) {
            return Category::options($id);
        });

All work fine for first and second category, but for category 3 I need to pass also the id of category 1.

If I have 3 selects that depend on each one, how I can pass more data via load() ajax for select2?

I have this:

        $form->select('categories')->options(
            Category::all()->pluck('name', 'id')
        )->load('categories2', '/admin/api/categories/secondary');

        $form->select('categories2')->options(function ($id) {
            return Category::options($id);
        })->load('categories3', '/admin/api/categories/tertiary');

        $form->select('categories3')->options(function ($id) {
            return Category::options($id);
        });

All work fine for first and second category, but for category 3 I need to pass also the id of category 1.

What about this scenario? what to do here ? I also need to pass category 1 value to category 3. How to do that ?

Any help will be much appreciated.
Thank you

Was this page helpful?
0 / 5 - 0 ratings