1. Go to this page and download the library: Download fleetbase/core-api 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/ */
fleetbase / core-api example snippets
namespace Fleetbase\MyExtension\Http\Transformers;
use Fleetbase\Contracts\PreparesResourceTransformation;
use Fleetbase\Http\Transformers\Transformer;
use Fleetbase\Models\User;
use Fleetbase\Support\ResourceTransformerContext;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Collection;
class UserBadgeTransformer extends Transformer implements PreparesResourceTransformation
{
protected static $target = User::class; // resource class, model class, interface, array of them, or '*'
protected static $priority = 10; // lower runs first; higher runs later and can override
protected static $contexts = ['http', 'webhook']; // null for every channel (http, webhook, broadcast)
protected static $only = 'internal'; // 'internal', 'public' or null for both
// Optional: runs once per resolve with every model about to be serialized, so you can batch-load.
public function prepare(Collection $models, Request $request, ResourceTransformerContext $context): void
{
$context->set('badges', Badge::whereIn('user_uuid', $models->pluck('uuid'))->get()->keyBy('user_uuid'));
}
public function transform(array $data, JsonResource $resource, Request $request, ResourceTransformerContext $context): array
{
return $data + ['badge' => $context->get('badges')[$resource->resource->uuid]->name ?? null];
}
}
class MyExtensionServiceProvider extends CoreServiceProvider
{
public $transformers = [
UserBadgeTransformer::class,
[OrderTotalsTransformer::class, ['priority' => 5]],
];
public function boot()
{
$this->registerTransformers(); // the $transformers property
$this->registerTransformersFrom(__DIR__ . '/../Http/Transformers'); // every ResourceTransformer in the directory
}
}