PHP code example of lunarphp / filament

1. Go to this page and download the library: Download lunarphp/filament 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/ */

    

lunarphp / filament example snippets


use Lunar\Filament\Forms\Components\ProductSelect;

ProductSelect::make('product_id')
    ->

use Lunar\Core\Models\Channel;
use Lunar\Filament\Forms\Components\ProductSelect;

ProductSelect::make('product_id')
    ->showSku()                                  // append " — {sku}" to each result
    ->scopeStatus('published')                   // only published products
    ->withinChannel(Channel::getDefault())       // only products attached to this channel
    ->excludeAttached()                          // hide records already on the surrounding relation
    ->multiple();                                // multi-select is supported on every selector

use Lunar\Filament\Forms\Components\ProductVariantSelect;

// Direct variant search (SKU + product name)
ProductVariantSelect::make('variant_id');

// Search products first, then list every variant of the matched product
ProductVariantSelect::make('variant_id')->searchViaProduct();

// Restrict to one product (e.g. a row already chose the parent)
ProductVariantSelect::make('variant_id')->forProduct($product);

use Lunar\Filament\Forms\Components\CollectionSelect;

CollectionSelect::make('collection_id')
    ->excludeDescendantsOf($currentCollection)   // safe re-parent target picker
    ->excludeSelf($currentCollection);

use Lunar\Filament\Forms\Components\CountrySelect;
use Lunar\Filament\Forms\Components\StateSelect;

CountrySelect::make('country_id')->live();
StateSelect::make('state')->dependsOn('country_id');

use Lunar\Core\Models\Brand;
use Lunar\Core\Models\Collection;
use Lunar\Core\Models\Product;
use Lunar\Core\Models\ProductVariant;
use Lunar\Filament\Forms\Components\DiscountTargetSelect;

DiscountTargetSelect::make('discountable')
    ->targets([Product::class, ProductVariant::class, Collection::class, Brand::class]);

use Filament\Actions\AttachAction;
use Filament\Forms\Components\Select;
use Lunar\Filament\Forms\Components\CollectionSelect;

AttachAction::make()
    ->recordSelect(fn (Select $select) => CollectionSelect::applyTo($select));

ProductSelect::make('product_id')
    ->modifyOptionsQueryUsing(fn ($query) => $query->whereHas('media'));

use Lunar\Core\Models\Product;
use Lunar\Filament\Forms\Components\Support\RecordSearch;

$results = RecordSearch::for(Product::class, $search)->take(20)->get();

use Lunar\Filament\Forms\Components\Attributes;

Attributes::make();                                          // attributes for the resource model
Attributes::make()->using(ProductVariant::class);            // attributes for a different model
Attributes::make()->relationship('variant');                 // load/save through a relationship

use Lunar\Filament\Forms\Components\TranslatedText as TranslatedTextInput;

TranslatedTextInput::make('name')->

use Lunar\Filament\Tables\Columns\TranslatedTextColumn;

TranslatedTextColumn::make('attribute_data.name')
    ->attributeData()
    ->limit(40)
    ->limitedTooltip();

use Lunar\Filament\Tables\Columns\ThumbnailImageColumn;

ThumbnailImageColumn::make('thumbnail')
    ->resolveThumbnailUrlUsing(fn ($record) => $record->getThumbnailImage());

use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Tables\Table;
use Lunar\Core\Models\Brand;
use Lunar\Filament\Schemas\Brand\BrandForm;
use Lunar\Filament\Schemas\Brand\BrandInfolist;
use Lunar\Filament\Tables\Brand\BrandTable;

class MyBrandResource extends Resource
{
    protected static ?string $model = Brand::class;

    public static function form(Schema $schema): Schema
    {
        return BrandForm::configure($schema);
    }

    public static function infolist(Schema $schema): Schema
    {
        return BrandInfolist::configure($schema);
    }

    public static function table(Table $table): Table
    {
        return BrandTable::configure($table);
    }
}

$schema->components([
    BrandForm::getNameFormComponent(),
    BrandForm::getDescriptionFormComponent(),
    \Filament\Forms\Components\Toggle::make('featured'),    // your own field
]);

use Lunar\Filament\Actions\Orders\CaptureOrderAction;
use Lunar\Filament\Actions\Orders\CloseOrderAction;
use Lunar\Filament\Actions\Orders\RefundOrderAction;

protected function getDefaultHeaderActions(): array
{
    return [
        CaptureOrderAction::make(),
        RefundOrderAction::make(),
        CloseOrderAction::make(),
    ];
}

use Lunar\Core\Actions\Orders\RefundOrder;

$result = RefundOrder::run(
    order: $order,
    transactionId: $transaction->id,
    amount: '25.00',
    notes: 'Customer requested partial refund',
);

use Filament\Resources\Resource;
use Lunar\Core\Models\Product;
use Lunar\Filament\GlobalSearch\Concerns\HasLunarGlobalSearch;
use Lunar\Filament\GlobalSearch\ProductGlobalSearch;

class MyProductResource extends Resource
{
    use HasLunarGlobalSearch;

    protected static ?string $model = Product::class;

    protected static string $globalSearch = ProductGlobalSearch::class;
}

use Lunar\Filament\Widgets\Dashboard\Orders\AverageOrderValueChart;
use Lunar\Filament\Widgets\Dashboard\Orders\LatestOrdersTable;
use Lunar\Filament\Widgets\Dashboard\Orders\NewVsReturningCustomersChart;
use Lunar\Filament\Widgets\Dashboard\Orders\OrdersSalesChart;
use Lunar\Filament\Widgets\Dashboard\Orders\OrderStatsOverview;
use Lunar\Filament\Widgets\Dashboard\Orders\OrderTotalsChart;
use Lunar\Filament\Widgets\Dashboard\Orders\PopularProductsTable;

return $panel->widgets([
    OrderStatsOverview::class,
    OrdersSalesChart::class,
    OrderTotalsChart::class,
    AverageOrderValueChart::class,
    NewVsReturningCustomersChart::class,
    LatestOrdersTable::class,
    PopularProductsTable::class,
]);

'record_urls' => [
    'order' => ManageOrder::class,                            // ManageOrder::getUrl(['record' => $record])
    'product_variant' => [MyProductResource::class, 'edit'],  // MyProductResource::getUrl('edit', ['record' => $record])
    'collection_edit' => null,                                // omit the link
],

use Lunar\Filament\FieldTypes\BaseFieldType;
use Lunar\Filament\Support\Facades\AttributeData;

class ColorPicker extends BaseFieldType
{
    public function getFilamentComponent($attribute, $component) { /* … */ }
    public function getCast(): string { /* … */ }
}

AttributeData::registerFieldType(ColorPicker::class);

use Lunar\Filament\Schemas\Product\ProductForm;
use Lunar\Filament\Support\Facades\LunarFilament;

LunarFilament::extensions([
    ProductForm::class => new class {
        public function configureForm($schema)
        {
            return $schema->components([
                ...$schema->getComponents(),
                Filament\Forms\Components\Toggle::make('featured'),
            ]);
        }
    },
]);

namespace App\Filament\Schemas\Product;

class ProductForm extends \Lunar\Filament\Schemas\Product\ProductForm
{
    public static function getBrandComponent(): \Filament\Schemas\Components\Component
    {
        return parent::getBrandComponent()->hidden();
    }
}

// In a service provider:
$this->app->bind(
    \Lunar\Filament\Schemas\Product\ProductForm::class,
    \App\Filament\Schemas\Product\ProductForm::class,
);

use Filament\Panel;
use Filament\PanelProvider;
use Lunar\Filament\Widgets\Dashboard\Orders\OrderStatsOverview;
use Lunar\Filament\Widgets\Dashboard\Orders\OrdersSalesChart;

class MyPanelProvider extends PanelProvider
{
    public function panel(Panel $panel): Panel
    {
        return $panel
            ->id('store')
            ->path('store')
            ->resources([
                MyBrandResource::class,        // uses Lunar\Filament\Schemas\Brand\BrandForm internally
                MyProductResource::class,
                MyOrderResource::class,
            ])
            ->widgets([
                OrderStatsOverview::class,
                OrdersSalesChart::class,
            ]);
    }
}
bash
php artisan migrate
bash
php artisan vendor:publish --tag=lunar-filament.schemas
bash
php artisan vendor:publish --tag=lunar-filament.config    # config file
php artisan vendor:publish --tag=lunar-filament.lang      # translation files (16 locales)
php artisan vendor:publish --tag=lunar-filament.views     # blade views