Skip to main content

SlugResource

The SlugResource transforms the Slug model into a structured JSON resource for API responses.

Namespace

JobMetric\Url\Http\Resources\SlugResource

Overview

The resource provides:

  • Structured output: Consistent JSON format
  • ISO 8601 timestamps: Standard datetime formatting
  • Related model: Slugable model resource (via event)

When to Use SlugResource

Use SlugResource when you need:

  • API responses: Return slug data in API endpoints
  • Structured output: Consistent JSON format for slugs
  • Related model data: Include slugable model information
  • Timestamp formatting: Standard ISO 8601 datetime format

Example scenarios:

  • API endpoints returning product slugs
  • Slug management interfaces
  • Admin panels showing slug information
  • API responses with slug details

Resource Structure

{
"slugable_type": "App\\Models\\Product",
"slugable_id": 1,
"slug": "macbook-pro-14",
"collection": "products",
"deleted_at": null,
"created_at": "2024-01-01T00:00:00.000000Z",
"updated_at": "2024-01-01T00:00:00.000000Z",
"slugable": {
// Resource from UrlableResourceEvent
}
}

Field Details

FieldTypeDescription
slugable_typestringModel class name (polymorphic)
slugable_idintModel ID (polymorphic)
slugstringURL-friendly slug (max 100 chars)
collectionstring|nullOptional collection name
deleted_atstring|nullSoft delete timestamp (ISO 8601)
created_atstringCreation timestamp (ISO 8601)
updated_atstringUpdate timestamp (ISO 8601)
slugablemixedRelated model resource (from event)

Usage

Basic Usage

use JobMetric\Url\Http\Resources\SlugResource;
use JobMetric\Url\Models\Slug;

$slug = Slug::find(1);
return new SlugResource($slug);

Via HasUrl Trait

$product = Product::find(1);

// Accessor
$resource = $product->slug_resource; // SlugResource instance

// Method
$result = $product->slug();
// Returns: ['ok' => true, 'data' => SlugResource]

In API Responses

namespace App\Http\Controllers;

use App\Models\Product;
use JobMetric\Url\Http\Resources\SlugResource;

class ProductController extends Controller
{
public function show(Product $product)
{
return response()->json([
'product' => $product->toArray(),
'slug' => new SlugResource($product->slug_resource),
]);
}
}

Complete Examples

Example 1: Single Slug Resource

$slug = Slug::find(1);
return new SlugResource($slug);

Example 2: Collection of Slug Resources

$slugs = Slug::where('collection', 'products')->get();
return SlugResource::collection($slugs);
// Listen to UrlableResourceEvent to provide related model resource
Event::listen(UrlableResourceEvent::class, function (UrlableResourceEvent $event) {
if ($event->urlable instanceof Product) {
$event->resource = new ProductResource($event->urlable);
}
});

$slug = Slug::find(1);
return new SlugResource($slug);
// slugable field will contain ProductResource

Example 4: API Endpoint

Route::get('/api/products/{product}/slug', function (Product $product) {
return new SlugResource($product->slug_resource);
});
  • Slug Model - Model that this resource transforms
  • HasUrl Trait - Trait that provides slug_resource accessor
  • Events - UrlableResourceEvent for related model