PHP code example of ngos / admin-core

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

    

ngos / admin-core example snippets


/** @return array<int, array<string, mixed>> */
protected function listFilters(): array
{
    return [
        ['column' => 'status', 'type' => 'select', 'label' => 'Status', 'options' => [...]],
        ['column' => 'created_at', 'type' => 'date', 'label' => 'Created'],
    ];
}

protected function listAggregates(): array
{
    return [
        'total' => ['fn' => 'sum', 'money' => true, 'currency' => null], // money → "៛125,000"
        'qty'   => 'sum',                                                 // plain numeric total
        // fn is one of sum | avg | min | max | count
    ];
}

protected function recordScope(): array
{
    return ['user_id' => auth()->id()];
}

protected function message(string $action): string
{
    return __("products.{$action}"); // $action is created|updated|deleted|restored
}

use Ngos\AdminCore\Actions\Action;

protected function resourceActions(): array
{
    return [
        Action::make('mark-paid')->label('Mark as paid')->icon('bi bi-cash')->color('success')->confirm()
            ->handle(fn ($records) => $records->each->update(['status' => 'paid'])),
    ];
}

protected function fieldPermissions(): array
{
    return ['status' => 'change-status-order', 'cost' => 'edit-cost-product'];
}

Action::make('refund')->ds) => $records->each->update(['status' => 'refunded']));

protected function transitions(): array
{
    return [
        Transition::make('post')->from('confirmed')->to('posted')->confirm()
            ->handle(fn ($record) => $record->postToStock()),
    ];
}

Transition::make('close')->from('open')->to('closed')
    ->form([
        'closing_counted' => [' 'method'          => ['rules' => ['

Transition::make('pay-in')->fromAny()
    ->form(['amount' => ['ass)->payIn($record, $input));

use Ngos\AdminCore\Concerns\LogsActivity;

class Order extends Model { use LogsActivity; }

foreach (glob(__DIR__ . '/Api/Modules/*.php') ?: [] as $module) {
    

$product->mediaIn('photos');        // ordered collection of attached files
$product->firstMediaUrl('cover');   // single hero-image url
$product->syncMedia([$ids], 'photos');

'widgets' => [
    \App\Dashboard\RevenueWidget::class,                                 // a class widget
    ['type' => 'stat', 'title' => 'Users', 'icon' => 'bi-people',        // an inline widget
     'value' => fn ($c) => \App\Models\User::query()->count(), 'link' => '/admin/users'],
    ['type' => 'chart', 'title' => 'Signups', 'col' => 6,
     'chart' => fn ($c) => ['type' => 'line', 'series' => [/* … */], 'categories' => [/* … */]]],
    ['type' => 'list', 'title' => 'Latest orders',
     'rows' => fn ($c) => [['label' => '…', 'meta' => '…', 'link' => '…']]],
],

use Ngos\AdminCore\Notifications\AdminNotification;
use Ngos\AdminCore\Notifications\Platform\NotificationPlatform;

NotificationPlatform::send(new AdminNotification(
    title:   'Order shipped',
    message: "Order #{$order->id} is on its way.",
    url:     route('admin.orders.show', $order), // followed when the row is clicked
    icon:    'bi-truck',                          // any Bootstrap icon (optional)
    extra:   ['order_id' => $order->id],          // optional extra payload keys
), $user);                                        // a user, an email/phone, or a collection of recipients

'translation' => [
    'driver'  => env('ADMIN_CORE_TRANSLATOR', 'mymemory'), // mymemory | libretranslate | null
    'locales' => ['en' => 'English', 'km' => 'ខ្មែរ', 'th' => 'ไทย'], // add as many as you like
    'default' => 'en',
],

  ->addColumn('actions', fn ($row) => $this->actions($row, 'order', [
      ['label' => 'Approve', 'url' => route('admin.orders.approve', $row->getRouteKey()),
       'icon' => 'bi bi-check2-circle', 'can' => 'edit-order'],
  ]))
  

  // app/Models/BaseModel.php
  abstract class BaseModel extends \Illuminate\Database\Eloquent\Model
  {
      use \Ngos\AdminCore\Concerns\HasPublicUuid;   // shared behaviour lives in traits
      protected $casts = ['published_at' => 'datetime'];
  }
  // config/admin-core.php → 'generator' => ['base_model' => \App\Models\BaseModel::class],
  

// routes/Web/Backend/Modules/products.php
Route::group(['prefix' => 'products', 'as' => 'products.'], function () {
    Route::crud('product', \App\Http\Controllers\Backend\ProductController::class);
});

class ProductController extends \Ngos\AdminCore\Http\Controllers\WebController { /* $service, $viewPath, $routeBase, $storeRequest, $updateRequest */ }
class ProductService    extends \Ngos\AdminCore\Services\BaseService          { /* $model */ }

public function create(array $data): Model
{
    $items = $data['items'] ?? null; unset($data['items']);
    $purchase = parent::create($data);
    $this->syncHasMany($purchase, 'items', $items, fn ($r) => empty($r['product_id']) ? null : [
        'product_id' => $r['product_id'],
        'qty'        => $r['qty'] ?? 0,
    ]);

    return $purchase;
}
bash
php artisan admin-core:install
bash
php artisan admin-core:field Product "sku:string^, discount:decimal?"
php artisan migrate
bash
php artisan admin-core:portal merchant
php artisan migrate
php artisan db:seed --class=MerchantSeeder   # creates [email protected] / password + a full-access role