tokarskimarcin/laravel-admin-ext-select-inline-create

v0.1.2 2020-05-15 11:01 UTC

This package is auto-updated.

Last update: 2024-09-15 20:28:05 UTC


README

preview 如果不存在,使用选择字段创建实体。创建的实体将被立即选中。

要求

  • "php": ">=7.2.0",
  • "encore/laravel-admin": "~1.6"
  • "tokarskimarcin/laravel-admin-ext-modal-form": "^0.1"

安装

发布

执行以下命令发布包。它将复制供应商资源到您的应用程序公共目录。

php artisan admin:select-inline-create:publish

更新

要更新/覆盖包的资产,请在发布命令中添加 --force 选项。

文档

用法

SelectInlineCreate字段可以通过在Admin/Form上执行selectInlineCreate方法使用。

use Encore\Admin\Auth\Database\Administrator;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;

Admin::form(new Administrator(), function (Form $form){
    $form->selectInlineCreate('manager', 'Manager');
    ...;
});

当实体被创建时,它通过在查询字符串中带有id参数的搜索url来查找选项。使用SelectResponse类和静态函数get来返回正确的响应。

use Encore\SelectInlineCreate\Form\SelectResponse;
use Illuminate\Support\Facades\Request;

class SearchController{
    public function search(){
        $id = Request::offsetGet('id');

        // SelectResponse::get($entity, $callback)
        // $entity can be null, then SelectResponse will return suitable message
        // in $callback return array with 'id' and 'text' keys. 
        // js will create an option based on this

        return SelectResponse::get(
            Administrator::query()->find($id),
            function(Administrator $administrator){
                return ['id' => $administrator->id, 'text' => $administrator->name];
            });
    }
}

方法

SelectInlineCreate具有与Admin中的常规Select相同的方法

1. setSearchUrl

如果不使用ajax(...)函数,则应使用setSearchUrl

//Example
$form->selectInlineCrate(...)
    ->setSearchUrl(route('manager.search'))
    ->options(...);

如果使用ajax(...),则setSearchUrl在ajax内部执行,搜索url与ajax相同。

$form->selectInlineCrate(...)
    ->ajax(route('manager.search'));

如果通过ajax函数设置,则可以覆盖搜索url。为此,必须在ajax之后执行setSearchUrl

$form->selectInlineCrate(...)
    ->ajax(route('manager.search-by-query'))
    ->setSearchUrl(route('manager.search-by-id'));