翻译进度
7
分块数量
0
参与人数

注册 Metrics

这是一篇协同翻译的文章,你可以点击『我来翻译』按钮来参与翻译。

Once you have defined a metric, you are ready to attach it to a resource. Each resource generated by Nova contains a cards method. To attach a metric to a resource, you should simply add it to the array of metrics / cards returned by this method:

/**
 * Get the cards available for the resource.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
public function cards(Request $request)
{
    return [new Metrics\UsersPerDay];
}

Resource Detail Metrics

In addition to placing metrics on the resource index screen, you may also attach a metric to the resource detail screen. For example, if you are building a podcasting application, you may wish to display the total number of podcasts created by users over time. To instruct a metric to be displayed on the detail page instead of the index page, chain the onlyOnDetail method onto your metric registration:

/**
 * Get the cards available for the request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
public function cards(Request $request)
{
    return [
        (new Metrics\PodcastCount)->onlyOnDetail(),
    ];
}

Of course, you will need to modify your metric's query to only gather metric data on the resource for which it is currently being displayed. To accomplish this, your metric's calculate method may access the resourceId property on the incoming $request:

use App\Podcast;

return $this->count(
    $request,
    Podcast::where('user_id', $request->resourceId)
);

Dashboard Metrics

You are not limited to displaying metrics on a resource's index screen. You are free to add metrics to your primary Nova "dashboard", which is the default page that Nova displays after login. By default, this screen displays some helpful links to the Nova documentation via the built-in Help card. To add a metric to your dashboard, add the metric to the array of cards returned by the cards method of your app/Providers/NovaServiceProvider class:

use App\Nova\Metrics\NewUsers;

/**
 * Get the cards that should be displayed on the Nova dashboard.
 *
 * @return array
 */
protected function cards()
{
    return [
        new NewUsers,
    ];
}

Metric Sizes

By default, metrics take up one-third of the Nova content area. However, you are free to make them larger. To accomplish this, call the width method when registering the metric with a resource:

/**
 * Get the cards available for the request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
public function cards(Request $request)
{
    return [
        // Two-thirds of the content area...
        (new Metrics\UsersPerDay)->width('2/3'),

        // Full width...
        (new Metrics\UsersPerDay)->width('full'),
    ];
}

Authorization

If you would like to only expose a given metric to certain users, you may chain the canSee method onto your metric registration. The canSee method accepts a Closure which should return true or false. The Closure will receive the incoming HTTP request:

use App\User;

/**
 * Get the cards available for the resource.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
public function cards(Request $request)
{
    return [
        (new Metrics\UsersPerDay)->canSee(function ($request) {
            return $request->user()->can('viewUsersPerDay', User::class);
        }),
    ];
}

In the example above, we are using Laravel's Authorizable trait's can method on our User model to determine if the authorized user is authorized for the viewUsersPerDay action. However, since proxying to authorization policy methods is a common use-case for canSee, you may use the canSeeWhen method to achieve the same behavior. The canSeeWhen method has the same method signature as the Illuminate\Foundation\Auth\Access\Authorizable trait's can method:

use App\User;

/**
 * Get the cards available for the resource.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
public function cards(Request $request)
{
    return [
        (new Metrics\UsersPerDay)->canSeeWhen(
            'viewUsersPerDay', User::class
        ),
    ];
}

本文章首发在 Laravel China 社区
本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

讨论数量: 0
发起讨论


暂无话题~