Directives | Laravel Feeds

Laravel Feeds Help

Directives

@attributes

You can use a key named @attributes to add attributes to a node:

namespace App\Feeds; use App\Feeds\Info\AttributesDirectiveFeedInfo; use App\Feeds\Items\AttributesDirectiveFeedItem; use App\Models\User; use DragonCode\LaravelFeed\Feeds\Feed; use DragonCode\LaravelFeed\Feeds\Info\FeedInfo; use DragonCode\LaravelFeed\Feeds\Items\FeedItem; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; class AttributesDirectiveFeed extends Feed { public function builder(): Builder { return User::query(); } public function info(): FeedInfo { return new AttributesDirectiveFeedInfo; } public function item(Model $model): FeedItem { return new AttributesDirectiveFeedItem($model); } }
namespace App\Feeds\Info; use DragonCode\LaravelFeed\Feeds\Info\FeedInfo; use function fake; class AttributesDirectiveFeedInfo extends FeedInfo { public function toArray(): array { return [ 'company' => [ '@attributes' => ['since' => fake()->year], ], 'url' => config('app.url'), ]; } }
namespace App\Feeds\Items; use DragonCode\LaravelFeed\Feeds\Items\FeedItem; class AttributesDirectiveFeedItem extends FeedItem { public function toArray(): array { return [ 'name' => $this->model->name, 'contact' => [ '@attributes' => [ 'email' => $this->model->email, 'phone' => '555-000-' . $this->model->id, ], ], ]; } }

This code will result in:

<?xml version="1.0" encoding="UTF-8"?> <attributes_directive> <company since="2003"/> <url>https://example.com</url> <user> <name>Prof. Jaeden Hodkiewicz</name> <contact email="[email protected]" phone="555-000-1"/> </user> <user> <name>Miss Annetta Frami MD</name> <contact email="[email protected]" phone="555-000-2"/> </user> </attributes_directive>

@value

You can use a key named @value to add value to a node:

namespace App\Feeds\Items; use DragonCode\LaravelFeed\Feeds\Items\FeedItem; class ValueDirectiveFeedItem extends FeedItem { public function toArray(): array { return [ 'name' => [ '@value' => $this->model->name, ], 'contact' => [ '@attributes' => ['type' => 'email'], '@value' => $this->model->email, ], ]; } }

This code will result in:

<?xml version="1.0" encoding="UTF-8"?> <value_directive> <user> <name>Arely Weber</name> <contact type="email">[email protected]</contact> </user> <user> <name>Cali Corkery PhD</name> <contact type="email">[email protected]</contact> </user> </value_directive>

@cdata

You can use a key named @cdata to add CDATA to a node:

namespace App\Feeds\Items; use DragonCode\LaravelFeed\Feeds\Items\FeedItem; use function sprintf; class CdataDirectiveFeedItem extends FeedItem { public function toArray(): array { return [ 'name' => [ '@cdata' => sprintf('<h1>%s</h1>', $this->model->name), ], 'email' => $this->model->email, ]; } }

This code will result in:

<?xml version="1.0" encoding="UTF-8"?> <cdata_directive> <user> <name><![CDATA[<h1>Mrs. Elsie Heathcote DVM</h1>]]></name> <email>[email protected]</email> </user> <user> <name><![CDATA[<h1>Davin Raynor MD</h1>]]></name> <email>[email protected]</email> </user> </cdata_directive>

@mixed

To insert XML fragments “as is”, use the @mixed directive:

namespace App\Feeds\Items; use DragonCode\LaravelFeed\Feeds\Items\FeedItem; class MixedDirectiveFeedItem extends FeedItem { public function toArray(): array { return [ 'name' => $this->model->name, '@mixed' => <<<XML <some> <first>Foo</first> <second>{$this->model->email}</second> </some> XML, ]; } }

This code will result in:

<?xml version="1.0" encoding="UTF-8"?> <mixed_directive> <user> <name>Veronica Hahn</name> <some> <first>Foo</first> <second>[email protected]</second> </some> </user> <user> <name>Brett Weber</name> <some> <first>Foo</first> <second>[email protected]</second> </some> </user> </mixed_directive>

@custom

In some cases, you need to place an array of elements with the same names.

To do this, add a @ symbol to the start of the key name:

namespace App\Feeds\Items; use DragonCode\LaravelFeed\Feeds\Items\FeedItem; use function fake; class ArrayDirectiveFeedItem extends FeedItem { public function toArray(): array { return [ 'name' => $this->model->name, '@avatar' => [ fake()->imageUrl(), fake()->imageUrl(), ], '@images' => [ [ '@attributes' => ['name' => fake()->words(asText: true)], '@value' => fake()->imageUrl(), ], [ '@attributes' => ['name' => fake()->words(asText: true)], '@value' => fake()->imageUrl(), ], ], ]; } }

This code will result in:

<?xml version="1.0" encoding="UTF-8"?> <array_directive> <user> <name>Brandi Shields</name> <avatar>https://via.placeholder.com/640x480.png/00ddaa?text=aut</avatar> <avatar>https://via.placeholder.com/640x480.png/005566?text=error</avatar> <images name="nemo cumque consequatur">https://via.placeholder.com/640x480.png/0066dd?text=eveniet</images> <images name="atque quaerat officiis">https://via.placeholder.com/640x480.png/0000aa?text=repudiandae</images> </user> <user> <name>Taya Vandervort</name> <avatar>https://via.placeholder.com/640x480.png/000011?text=deserunt</avatar> <avatar>https://via.placeholder.com/640x480.png/000088?text=iusto</avatar> <images name="dolor cum perferendis">https://via.placeholder.com/640x480.png/00eecc?text=vero</images> <images name="est vitae excepturi">https://via.placeholder.com/640x480.png/009955?text=ratione</images> </user> </array_directive>
05 September 2025