PHP code example of internetguru / laravel-model-browser

1. Go to this page and download the library: Download internetguru/laravel-model-browser 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/ */

    

internetguru / laravel-model-browser example snippets


model="App\Models\User"
model="App\Models\User@summary"

:viewAttributes="[
    'created_at' => __('summary.created_at'),
    'name' => __('summary.name'),
    'email' => __('summary.email'),
]"

:viewAttributes="['created_at' => __('summary.created_at'), 'customer.name' => __('summary.ordered_by')]"
:exportAttributes="[
    'customer.name' => __('summary.ordered_by'),
    'customer.email' => __('summary.ordered_by_email'),
]"

:formats="[
    'created_at' => 'formatDateTime',
    'price' => 'formatCurrency',
    'symbol' => 'formatOrderSymbol',
    'payment_type' => 'formatTransactionPaymentType',
]"

function formatDateTime($value, $item)
{
    return \Carbon\Carbon::parse($value)->format('d.m.Y H:i');
}

function formatCurrency($value, $item)
{
    return number_format($value / 100, 2) . ' CZK';
}

:viewAttributes="['customer' => __('summary.buyer'), 'author' => __('summary.issuer')]"
:formats="['customer' => 'formatUser', 'author' => 'formatUser']"
:rawFormats="['customer' => 'rawUser', 'author' => 'rawUser']"

function formatUser(User $user): string
{
    return sprintf('<a href="%s">%s</a>', route('users.show', $user), e($user->name));
}

function rawUser(User $user): string
{
    return sprintf('%s <%s>', $user->name, $user->email);
}

:alignments="[
    'created_at' => 'start',
    'amount' => 'end',
    'is_active' => 'center',
]"

defaultSortColumn="created_at"
defaultSortDirection="desc"

:enableSort="false"

:refreshInterval="10"

exportName="payments"

:exportLimit="2000"

:statsAttributes="['price', 'credit']"
:statsLimit="1000"

:statsTextAttributes="['symbol']"

:lightDarkStep="2"

:columnWidths="[
    'name' => 'minmax(8em, 2fr)',
    'email' => 'minmax(10em, 2fr)',
    'is_active' => '6em',
]"

:filters="[
    'created' => [
        'type' => 'date',
        'label' => 'Created',
        'column' => 'created_at',
        'timezone' => 'Europe/Prague',
    ],
    'symbol' => [
        'type' => 'string',
        'label' => 'Symbol',
        'column' => 'symbol',
        'rules' => 'nullable|string|max:20',
    ],
    'voucher' => [
        'type' => 'string',
        'label' => 'Voucher',
        'column' => 'ulid',
        'relation' => 'charges.voucher',
        'rules' => 'nullable|string|max:32',
        'url' => 'voucher',
    ],
    'price' => [
        'type' => 'number',
        'label' => 'Price',
    ],
    'name' => [
        'type' => 'string',
        'label' => 'Customer',
        'column' => 'name',
        'relation' => 'customer',
    ],
]"
filterSessionKey="order-browser-filters"

'customer' => [
    'type' => 'string',
    'label' => 'Customer',
    'relation' => 'customer',
    'rules' => 'nullable|string|max:60',
    'columns' => ['name', ['column' => 'email', 'ascii_fast' => true]],
],

'party' => [
    'type' => 'string',
    'label' => 'Party',
    'columns' => [
        ['column' => 'name', 'relation' => 'customer'],
        ['column' => 'name', 'relation' => 'author'],
    ],
],

// Auto-applied filter (no manual code needed):
'name' => ['type' => 'string', 'label' => 'Name', 'column' => 'name']

// Manual filter (applied in your model scope via HasModelBrowserFilters):
'price' => ['type' => 'number', 'label' => 'Price']

use Internetguru\ModelBrowser\Traits\HasModelBrowserFilters;

class Order extends Model
{
    use HasModelBrowserFilters;

    protected string $modelBrowserFilterSessionKey = 'order_filter';

    public static function summary()
    {
        $query = static::with(['customer', 'payment', 'charges']);
        $price = (new static)->getModelBrowserFilterRange('price');

        // Manual filter: price is a computed sum of related charges
        if ($price['from'] !== null) {
            $query->whereRaw(
                '(SELECT SUM(amount) FROM charges WHERE charges.order_id = orders.id) >= ?',
                [$price['from'] * 100]
            );
        }
        if ($price['to'] !== null) {
            $query->whereRaw(
                '(SELECT SUM(amount) FROM charges WHERE charges.order_id = orders.id) <= ?',
                [$price['to'] * 100]
            );
        }

        return $query;
    }
}

'status' => [
    'type' => 'options',
    'label' => 'Status',
    'column' => 'status',
    'url' => 'filter-status',
    'options' => ['active' => 'Active', 'inactive' => 'Inactive'],
]

<livewire:table-model-browser
    model="App\Models\Order"
    :viewAttributes="['symbol' => 'Symbol', 'price' => 'Total', 'customer.name' => 'Ordered by']"
    :formats="['price' => 'formatCurrency']"
    :statsAttributes="['price']"
/>
sh
    php artisan vendor:publish --tag=config --provider="Internetguru\ModelBrowser\ModelBrowserServiceProvider"
    php artisan vendor:publish --tag=views --provider="Internetguru\ModelBrowser\ModelBrowserServiceProvider"
    php artisan vendor:publish --tag=translations --provider="Internetguru\ModelBrowser\ModelBrowserServiceProvider"

    # If you want to publish everything, you can use the `--provider` option:
    php artisan vendor:publish --provider="Internetguru\ModelBrowser\ModelBrowserServiceProvider"