Laravel-admin-select2 可能是第一个基于 The 996 Prohibited License 的项目

Laravel-Admin select2 插件

一款异步的 select2 针对 laravel-admin 插件,适用于不定条数的模型中选择框场景,包括单选、多选。

在表单中尝试检索时,才会 ajax 去模型中查询选项,设计极度精简。

*. 注意: 1.x 版与 0.x 版本使用和LICENSE不兼容.

安装

composer require xiaohuilam/laravel-admin-select2

使用

代码

<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
//use Encore\Admin\Form; // 废弃此行
use LaravelAdminExt\Select2\Form; // 启用此行
use App\Models\User;
use App\Models\UserResource;
use Illuminate\Support\Facades\DB;

class YourController extends Controller
{
    // ...
    protected function form()
    {
        $form = new Form(new UserResource);

        $form->select('user_id', 'User id')->match(function ($keyword) {
            /**
             * @var \Illuminate\Database\Eloquent\Builder $query 查询对象,**切记如果数据模型没有text或id属性,记得as成text和id!**
             */
            $query = User::where('name', 'LIKE', '%' . $keyword . '%')->select([DB::raw('name AS text'), 'id',]);
            return $query;
        })->text(function ($id) {
            return User::where(app(User::class)->getKeyName(), $id)->pluck('name', 'id');
        });

        $form->multipleSelect('tags', 'Tags')->match(
            function ($keyword) {
                return Tag::where('name', 'LIKE', '%' . $keyword . '%')->select([DB::raw('name AS text'), 'id',]);
            }
        )
        ->text(
            function ($id_list) {
                return Tag::whereIn(app(Tag::class)->getKeyName(), $id_list)->pluck('name', 'id');
            }
        );

        $form->text('title', 'Title');
        $form->textarea('content', 'Content');

        return $form;
    }
}

截图

screenshot.png

项目地址 https://github.com/xiaohuilam/laravel-admi...

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 1
xcaptain

支持拖拽排序吗,觉得这功能还挺必要的

4年前 评论
XiaohuiLam (楼主) 4年前
Su 4年前
xcaptain (作者) 4年前
XiaohuiLam (楼主) 4年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!