twentyonetf/ajax-table-card

一个可定制的Laravel Nova卡片,通过AJAX调用获取数据。

0.0.9 2023-03-27 22:26 UTC

This package is auto-updated.

Last update: 2024-09-28 01:47:10 UTC


README

描述

一个可定制的Laravel Nova卡片,通过AJAX调用获取数据。

为什么?

为了允许在仪表板或其他您选择的任何地方显示某些数据,同时通过AJAX调用获取数据以减少页面初始加载时间。

安装说明

composer require twentyonetf/ajax-table-card

如何实现?

  1. 创建一个继承自 AjaxTableCard 的类
<?php

namespace App\Nova\Cards;

use Twentyonetf\AjaxTableCard\AjaxTableCard;

class NewUsers extends AjaxTableCard
{
    /**
     * The width of the card (1/3, 1/2, or full).
     *
     * @var string
     */
    public $width = 'full';

    public string $title = 'New Users';

    public array $header = [
        'ID', 'Name', 'Email', 'View'
    ];

    public bool $expanded = true;

    public bool $link = '/api/new-users;
}

您还可以使用以下Artisan命令

php artisan card:create {CardClassName}
  1. 注册卡片,就像注册任何nova卡片一样,例如:如果您想在仪表板上使用它,则使用 NovaServiceProvider
    /**
     * Get the cards that should be displayed on the Nova dashboard.
     *
     * @return array
     */
    protected function cards(): array
    {
        return [
            (new NewUsers())
        ];
    }
  1. 生成并传递数据到卡片。

Route::get('/api/new-users', function () {

    $users = User::today()->get();
    
    $data = $users->map(function ($user) {
        return [
            $session->id,
            $user->name,
            $user->email,
            "/admin/resources/users/{$user->id}"
        ];
    });

    return $data;
})

定制

截图