Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Url Ai Transformer Laravel Package

spatie/laravel-url-ai-transformer

Laravel package to transform URLs and their web content with AI. Extract structured data (JSON-LD), generate summaries, images, or custom outputs via transformers and prompts. Runs via an Artisan command and stores results in the database for later retrieval.

View on GitHub
Deep Wiki
Context7

title: Writing your own transformers weight: 3

The real power of this package comes from writing your own transformers. Let's explore how to create custom transformers that fit your specific needs.

Creating a basic transformer

All transformers extend the Transformer base class and implement two required methods transform and getPrompt.

// app/Transformers/SummaryTransformer.php
namespace App\Transformers;

use Spatie\LaravelUrlAiTransformer\Transformers\Transformer;
use Prism\Prism\Prism;
use Spatie\LaravelUrlAiTransformer\Support\Config;

class SummaryTransformer extends Transformer
{
    public function transform(): void
    {
        $response = Prism::text()
            ->using(Config::aiProvider(), Config::aiModel())
            ->withPrompt($this->getPrompt())
            ->asText();

        // you should set the result property on the transformation result model to store the result
        $this->transformationResult->result = $response->text;
    }

    public function getPrompt(): string
    {
        return "Summarize this webpage content in 3 concise bullet points: \n\n" 
            . $this->urlContent;
    }
}

You can now use your transformer:

Transform::urls('https://example.com/article')
    ->usingTransformers(new SummaryTransformer);

When a transformer runs, it has access to three properties:

  • $this->url - The URL being transformed
  • $this->urlContent - The fetched content from the URL
  • $this->transformationResult - The database model where you store results

Custom transformer types

By default, the transformer type is derived from the class name. You can override this:

class MyCustomTransformer extends Transformer
{
    public function type(): string
    {
        return 'customType';
    }

    // Other methods...
}

You can use your custom type when retrieving a transformation result:

$ldJsonData = TransformationResult::forUrl('https://spatie.be/blog', 'customType');

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai