PHP code example of lupennat / nova-better-lens

1. Go to this page and download the library: Download lupennat/nova-better-lens library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lupennat / nova-better-lens example snippets


// in app/Nova/Resource.php

use Lupennat\BetterLens\ResolvesBetterLenses;

class User extends Resource
{
    use ResolvesBetterLenses;

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


use Lupennat\BetterLens\BetterLens;

class MostValuableUsers extends Lens
{
    use BetterLens;
}


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

class MostValuableUsers extends Lens
{
    use BetterLens;

    public function authorizedToView(Request $request)
    {
        return 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;
    }

}


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

class MostValuableUsers extends Lens
{
    use BetterLens;

    /**
     * Load lens only if viaResource request match resources.
     *
     * @return array<int, class-string<\Laravel\Nova\Resource>>
     */
    public static function withRelated() {
        return [Post::class, Video::class];
    }

}


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<int, int>
     */
    public static function perPageOptions()
    {
        return [10, 20, 30];
    }

}


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;
    }

}


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;
    }

}


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

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

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

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)
        ];
    }
}

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)
            ])
        ));
    }
}