PHPackages                             lupennat/nova-better-lens - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. lupennat/nova-better-lens

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

lupennat/nova-better-lens
=========================

Laravel Nova - Better Lens

v2.0.1(1y ago)73.5k—4.2%2[1 PRs](https://github.com/Lupennat/nova-better-lens/pulls)MITVuePHP ^7.4|^8.0

Since Jul 24Pushed 1y ago2 watchersCompare

[ Source](https://github.com/Lupennat/nova-better-lens)[ Packagist](https://packagist.org/packages/lupennat/nova-better-lens)[ RSS](/packages/lupennat-nova-better-lens/feed)WikiDiscussions main Synced 2d ago

READMEChangelogDependencies (2)Versions (10)Used By (0)

1. [Requirements](#Requirements)
2. [Installation](#Installation)
3. [Usage](#Usage)
4. [Trait Improvements](#trait-improvements)
    1. [Authorization](#authorization)
    2. [Hide From Toolbar](#hide-from-toolbar)
    3. [With Related](#with-related)
    4. [Per Page](#per-page)
    5. [Decorate Collection](#decorate-collection)
    6. [Resource Link Parameters](#resource-link-parameters)
    7. [Create Link Parameters](#create-link-parameters)
    8. [Breadcrumbs](#breadcrumbs)
5. [Many Fields](#many-fields)
6. [Lens Field](#lens-field)

Requirements
------------

[](#requirements)

- `php: ^7.4 | ^8`
- `laravel/nova: ^4`

Installation
------------

[](#installation)

```
composer require lupennat/nova-better-lens:^2.0

```

NOVAPACKAGE&lt;4.29.51.x&gt;4.29.62.xUsage
-----

[](#usage)

Laravel Nova Better Lens, improve [Nova Lenses](https://nova.laravel.com/docs/4.0/lenses/defining-lenses.html) Behaviours

```
// in app/Nova/Resource.php

use Lupennat\BetterLens\ResolvesBetterLenses;

class User extends Resource
{
    use ResolvesBetterLenses;

    public function lenses(NovaRequest $request)
    {
        return [
            new Lenses\MostValuableUsers,
        ];
    }
}
```

Include trait on Lens.

```
use Lupennat\BetterLens\BetterLens;

class MostValuableUsers extends Lens
{
    use BetterLens;
}
```

Trait Improvements
------------------

[](#trait-improvements)

### Authorization

[](#authorization)

Better Lens provide the ability to define authorization for lens. You can override parent resource methods using standard laravel authorization methods.

```
use Lupennat\BetterLens\BetterLens;
use Illuminate\Http\Request;

class MostValuableUsers extends Lens
{
    use BetterLens;

    public function authorizedToView(Request $request)
    {
        return false;
    }
}
```

#### Authorize To Create

[](#authorize-to-create)

By Default Nova Lens does not provide the ability to Create/Attach resource, to mantain compatibility `authorizedToCreate` will work only if lens will be loaded with many\* Fields or if the method is explicitly defined in the lens.

### Hide From Toolbar

[](#hide-from-toolbar)

Better Lens provide the ability to exclude a lens from parent resource toolbar list by the static method `hideFromToolbar` (by default is false).

```
use Lupennat\BetterLens\BetterLens;
use Illuminate\Http\Request;

class MostValuableUsers extends Lens
{
    use BetterLens;

    /**
     * Hide lens from resource toolbar.
     *
     * @return bool
     */
    public static function hideFromToolbar() {
        return true;
    }

}
```

### With Related

[](#with-related)

Better Lens provide the ability to make lens Viewable only as related resource.
By default all lens on Nova can be linked on Menu and can be loaded as a standalone index resource. With static method `withRelated`, you can override default behaviour and prevent lens to be loaded without a `viaResource` request (by default is empty array).

```
use Lupennat\BetterLens\BetterLens;
use Illuminate\Http\Request;

class MostValuableUsers extends Lens
{
    use BetterLens;

    /**
     * Load lens only if viaResource request match resources.
     *
     * @return array
     */
    public static function withRelated() {
        return [Post::class, Video::class];
    }

}
```

### Per Page

[](#per-page)

Better Lens provide the ability to define `perPage` options for lens. Using static methods `perPageViaRelationship` and `perPageOptions` you can override parent resource `perPage` options (by default use parent resource options).

```
use Lupennat\BetterLens\BetterLens;
use Illuminate\Http\Request;

class MostValuableUsers extends Lens
{
    use BetterLens;

    /**
     * The number of resources to show per page via relationships.
     *
     * @return int
     */
    public static function perPageViaRelationship()
    {
        return 5;
    }

    /**
     * The pagination per-page options configured for this resource.
     *
     * @return array
     */
    public static function perPageOptions()
    {
        return [10, 20, 30];
    }

}
```

you can also choose to show pagination filter when lens are loaded as relation via static `showPaginationViaRelationship` method (by default is `false`).

```
use Lupennat\BetterLens\BetterLens;
use Illuminate\Http\Request;

class MostValuableUsers extends Lens
{
    use BetterLens;

    /**
     * Show pagination filter when via relationship.
     *
     * @return bool
     */
    public static function showPaginationViaRelationship()
    {
        return true;
    }

}
```

### Decorate Collection

[](#decorate-collection)

Better Lens provide the ability to manipulate the collection of models returned by the query, you can override the static method `decorateCollection` and change the collection before it will be used to serialization (by default it return the original collection).

> Returned Collection should always return a Collection of Eloquent models, returning other types can generate runtime errors.

```
use Lupennat\BetterLens\BetterLens;
use Illuminate\Http\Request;

class MostValuableUsers extends Lens
{
    use BetterLens;

    /**
     * Decorate collection before returning data.
     *
     * @param \Illuminate\Support\Collection $models
     *
     * @return \Illuminate\Support\Collection
     */
    public static function decorateCollection(LensRequest $request, $models)
    {
        // do stuff ...
        return $models;
    }

}
```

### Resource Link Parameters

[](#resource-link-parameters)

Better Lens provide the ability to define extra parameters for view and edit url. You can define a list of `parameter => value` using static method `resourceLinkParameters` (by Default is empty array).

```
    /**
     * Url Extra Parameters.
     *
     * @param \Illuminate\Database\Eloquent\Model $model
     *
     * @return array
     */
    public static function resourceLinkParameters($model, LensRequest $request)
    {
        return [
            'name' => $model->name
        ];
    }
```

### Create Link Parameters

[](#create-link-parameters)

Better Lens provide the ability to define extra parameters for creation url. You can define a list of `parameter => value` using static method `createLinkParameters` (by Default is empty array).

```
    /**
     * Creation Url Extra Parameters.
     *
     * @return array
     */
    public static function createLinkParameters(NovaRequest $request)
    {
        return [
            'param' => 'value'
        ];
    }
```

### Breadcrumbs

[](#breadcrumbs)

Better Lens provide the ability to override default Breadcrumbs for Lens pages using method `breadcrumbs` (by Default Nova Lens page breadcrumbs are loaded).

```
    /**
     * Page Breadcrumbs.
     *
     * @return \Laravel\Nova\Menu\Breadcrumbs
     */
    public function breadcrumbs(LensRequest $request)
    {
        return Breadcrumbs::make([]);
    }
```

Many Fields
-----------

[](#many-fields)

Better Lens automatically enable a new method `lens` for all Many Relationship Fields:

- BelongsToMany
- HasMany
- HasManyThrough
- MorphedByMany
- MorphMany
- MorphToMany

many relationship will load the lens view instead of the main resource.

```
use Laravel\Nova\Fields\HasMany;
use Laravel\Nova\Http\Requests\NovaRequest;

class User extends Resource
{

    public function fields(Request $request)
    {
        return [
            HasMany::make('User Post', 'posts', Post::class)->lens(MostValuablePosts::class)
        ];
    }
}
```

Lens Field
----------

[](#lens-field)

Better Lens provide a new field `Lens` that can be used to load a resource lens without a "real many relationship".
By the field `Lens` it is possible to create a related table without creating a Custom ResourceTool.

### Example

[](#example)

We want to load a table with all translation key and the related translation value for current website. The table `translation_keys` does not have a real relation with the website resource and only contains all the translation keys. The table `translations` instead contains both the relation with key and website.
We don't want to use directly the `translations` table because we will lost the key not translated for the current website, we need to start from `translation_keys` table and load the website value from the `translations` table if present.

```
use Laravel\Nova\Http\Requests\NovaRequest;
use Lupennat\BetterLens\Fields\Lens;

class Website extends Resource
{

    public function fields(Request $request)
    {
        return [
           Lens::make(__('Translations'), TranslationKey::class, TranslationKeyForWebsite::class)->collapsedByDefault(),
        ];
    }
}
```

```
use Lupennat\BetterLens\BetterLens;
use Illuminate\Http\Request;
use Laravel\Nova\Fields\Text;

class TranslationKeyForWebsite extends Lens
{
    use BetterLens;

    public function fields(Request $request)
    {
        return [
           Text::make(__('key'), 'key'),
           Text::make(__('Value'), 'value')
        ];
    }

    /**
     * Get the query builder / paginator for the table.
     *
     * @param \Illuminate\Database\Eloquent\Builder $query
     *
     * @return mixed
     */
    public static function query(LensRequest $request, $query)
    {
        return $request->withOrdering($request->withFilters(
            $query->addSelect([
                'value' => Translation::whereColumn('translation_keys.id', 'translation.translation_key_id')
                            ->where('translation.website_id', $request->viaResourceId)
            ])
        ));
    }
}
```

###  Health Score

35

—

LowBetter than 77% of packages

Maintenance33

Infrequent updates — may be unmaintained

Popularity29

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~57 days

Recently: every ~66 days

Total

8

Last Release

674d ago

Major Versions

v1.2.1 → v2.0.02023-12-09

v1.3.0 → v2.0.12024-08-29

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/6160171?v=4)[Claudio Pennati](/maintainers/lupennat)[@Lupennat](https://github.com/Lupennat)

---

Top Contributors

[![Lupennat](https://avatars.githubusercontent.com/u/6160171?v=4)](https://github.com/Lupennat "Lupennat (27 commits)")

---

Tags

laravelnova

###  Code Quality

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/lupennat-nova-better-lens/health.svg)

```
[![Health](https://phpackages.com/badges/lupennat-nova-better-lens/health.svg)](https://phpackages.com/packages/lupennat-nova-better-lens)
```

###  Alternatives

[ebess/advanced-nova-media-library

Laravel Nova tools for managing the Spatie media library.

6163.5M22](/packages/ebess-advanced-nova-media-library)[optimistdigital/nova-sortable

This Laravel Nova package allows you to reorder models in a Nova resource's index view using drag &amp; drop.

2852.1M6](/packages/optimistdigital-nova-sortable)[outl1ne/nova-sortable

This Laravel Nova package allows you to reorder models in a Nova resource's index view using drag &amp; drop.

2862.1M9](/packages/outl1ne-nova-sortable)[markwalet/nova-modal-response

A Laravel Nova asset for Modal responses on an action.

17878.9k](/packages/markwalet-nova-modal-response)[advoor/nova-editor-js

A Laravel Nova field bringing EditorJs magic to Nova.

92219.3k3](/packages/advoor-nova-editor-js)[outl1ne/nova-page-manager

Page(s) and region(s) manager for Laravel Nova.

17947.0k](/packages/outl1ne-nova-page-manager)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
