PHP code example of calvient / arbol

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

    

calvient / arbol example snippets


return [
    'user_model' => 'App\Models\User',
    'series_path' => app_path('Arbol'),
];

public function scopeArbol($query)
{
    return $query->where('is_admin', true);
}



namespace App\Arbol;

use Calvient\Arbol\Contracts\IArbolSeries;
use Calvient\Arbol\DataObjects\ArbolBag;

class PodcastStreamsSeries implements IArbolSeries
{
    public function name(): string
    {
        return 'Podcast Streams';
    }

    public function description(): string
    {
        return 'All podcast streaming data';
    }

    public function data(ArbolBag $arbolBag, $user = null): array
    {
        // Build your query
        $query = PodcastStream::query();

        // Apply filters from the ArbolBag
        $arbolBag->applyQueryFilters($query, $this->filters());

        // The $user parameter contains the authenticated user (if any)
        // You can use this for user-specific data filtering
        if ($user) {
            $query->where('user_id', $user->id);
        }

        return $query->get()->toArray();
    }

    public function slices(): array
    {
        return [
            'State' => fn($row) => $row['state'],
            'Month' => fn($row) => date('Y-m', strtotime($row['created_at'])),
            'Source' => fn($row) => $row['source'],
        ];
    }

    public function filters(): array
    {
        return [
            'Time Period' => [
                'Last 7 Days' => fn($query) => $query->where('created_at', '>=', now()->subDays(7)),
                'Last 30 Days' => fn($query) => $query->where('created_at', '>=', now()->subDays(30)),
                'Last Year' => fn($query) => $query->where('created_at', '>=', now()->subYear()),
            ],
            'Listen Length' => [
                '< 15 minutes' => fn($query) => $query->where('listen_length', '<', 15),
                '>= 15 minutes' => fn($query) => $query->where('listen_length', '>=', 15),
            ],
        ];
    }

    public function aggregators(): array
    {
        return [
            'Default' => fn($rows) => count($rows),
            'Total Listen Time' => fn($rows) => collect($rows)->sum('listen_length'),
            'Average Listen Time' => fn($rows) => collect($rows)->avg('listen_length'),
        ];
    }
}

public function data(ArbolBag $arbolBag, $user = null): array
{
    $query = MyModel::query();

    // Check if a specific filter is selected
    if ($arbolBag->isFilterSet('Time Period', 'Last 7 Days')) {
        $query->where('created_at', '>=', now()->subDays(7));
    }

    // Or apply all filters automatically
    $arbolBag->applyQueryFilters($query, $this->filters());

    // Get the selected slice
    $slice = $arbolBag->getSlice();

    return $query->get()->toArray();
}
bash
php artisan vendor:publish --provider="Inertia\ServiceProvider" --force
php artisan view:clear
bash
php artisan vendor:publish --provider="Calvient\Arbol\ArbolServiceProvider"
bash
php artisan migrate
bash
php artisan vendor:publish --provider='Calvient\Arbol\ArbolServiceProvider'
php artisan migrate
bash
@php artisan vendor:publish --tag=arbol-assets --ansi --force
bash
php artisan make:arbol-series PodcastStreams
bash
php artisan arbol:clear
bash
php artisan arbol:clear --section=1
bash
php artisan make:arbol-series MyNewSeries